2012-03-22 15 views
1

私はアンドロイド2.3.3のアンドロイドソケットクライアントアプリケーションを作っています。 これはXML要求を送信し、応答をcobolソケットサーバーから受信します。ProgressDialogは消えません! Nullpointer例外

クライアントアプリケーションがサーバーに接続してデータを送信しているときに、progressdialogがポップアップ表示されます。

そして、それがポップアップしない(少し遅れが)、私は にNullPointerExceptionを得ることなく、それを却下カント

マイコード:

public class OctopusActivity extends Activity implements OnClickListener 
{ 
    //My variables 
    ProgressDialog progressDialog; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btn1 = (Button) findViewById(R.id.button1); 
     btn1.setOnClickListener(this); 

     Varenummer = (EditText) findViewById(R.id.editText1); 
     Varetekst = (EditText) findViewById(R.id.editText2); 
     Gruppe = (EditText) findViewById(R.id.editText3); 
     Producent = (EditText) findViewById(R.id.editText4); 
     Enhed = (EditText) findViewById(R.id.editText5); 
     Pris = (EditText) findViewById(R.id.editText6); 

    } 
    class ClientThread implements Runnable 
    { 


     public void run() 

     { 

      try 
      { 
       Korer = true; 
      Log.d("Nicklas", "Thread Igang"); 
      Socket socket = new Socket(serverIpAddress, serverPort); 
      socket.setSoTimeout(5000); 
      Log.d("Nicklas", "socket lavet"); 

        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); 
        Log.d("Nicklas", "Printer Oprettet"); 

        String request = ("XML Request"); 
        out.println(request); 
        out.flush(); 

        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
        Log.d("Nicklas", "Reader Oprettet"); 
        Vector<String> v = new Vector<String>(); 
        int i = 0; 
        Boolean KeepGoing = true; 
        while (KeepGoing) 
        { 

         try 
         { 
          String lol = in.readLine(); 
          if (lol.contains("</Answer>")) { KeepGoing = false; } 
          v.add(new String(lol)); 
          i++; 
         } 
         catch (Exception e) 
         { 
          Log.d("NickEEEXX", e.toString()); 
          KeepGoing = false; 
         }      
        } 

        Log.d("Trolo", String.valueOf(i)); 

        in.close(); 
        out.close(); 
        socket.close(); 

        String[] InputLinie = new String[i]; 

        v.toArray(InputLinie); 

        // This is where i treat the data 

        Korer = false; 
        Log.d("NicklasMEH", "KAgemand!"); 
        // The error comes on the following line: 
        progressDialog.dismiss(); 

      } 
      catch (Exception e) 
      { 
       Log.d("NicklasEx", e.toString()); 
       varetekst = "Kunne ikke forbinde til server"; 
       Korer = false; 

      } 

     } 

    } 
    @Override 
    public void onClick(View v) 
    { 
     // TODO Auto-generated method stub 
      Varetekst.setText(""); 
      Gruppe.setText(""); 
      Producent.setText(""); 
      Enhed.setText(""); 
      Pris.setText(""); 
      Log.d("Nicklas", "Starter thread"); 
      progressDialog.show(this, "", "Loading"); 

      if (!Korer) 
      { 
       Thread thread = new Thread(new ClientThread()); 
       thread.start(); 

      } 

    } 

} 

だから私の質問は以下のとおりです。 1. dialogprocessは少し遅れをポップアップ表示されます。私はそれがポップアップしたい、それからスレッドを開始したい。 2.なぜprogressDialog.dismiss();でnullpointer例外が発生しますか?

どのようなヘルプもうまくいくでしょう。

答えて

2

progressDialogをどこに配置しますか?

progressDialog =ProgressDialog.show(OctopusActivity.this,"title","message"); 
+1

変数はonCreateの上に宣言されます。 その後、.show(およびスレッド)がonClickメソッドで呼び出されます。 – user1285334

+2

あなたのラインはちょうどそれを修正しました! 変更されました 'progressDialog.show(this、" "、" Loading "); ' 〜 ' progressDialog = ProgressDialog.show(OctopusActivity.this、 "title"、 "message"); 'そして今それは完璧に動作します!ありがとうございました – user1285334

+1

はい!そのコメントを投稿してくれてありがとう。それが私の必要なものです。 –

関連する問題