2014-05-06 41 views
0

我想每1秒調用一次AsyncTask內部處理程序,但我一直在收到RuntimeException.Anybody知道如何解決這個問題? 下面是我的代碼:在處理程序中調用asynctask

package com.example.whatsapp; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import android.os.Build; 

public class MainActivity extends ActionBarActivity { 

    Intent contacts; 
    EditText t; 
    int counter=0; 
    Handler handler = new Handler(); 
    Runnable runnable = new Runnable() { 
     public void run() { 
      new AsyncReceiveMessage().execute(); 
      //afficher(); 
     } 
    }; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     contacts = new Intent(this, ContactsList.class); 
     runnable.run(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public void afficher() 
    { 
     counter+=1; 
     System.out.println("Hello " +counter); 
     handler.postDelayed(runnable, 1000); 
    } 

    public void registerUser(View view) 
    { 
     t = (EditText) findViewById(R.id.editText1); 
     Button b = (Button) findViewById(R.id.button1); 
     b.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if(t.getText().length()==11) 
       { 
        Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show(); 
        contacts.putExtra("deviceNumber", t.getText().toString()); 
        startActivity(contacts); 
        new AsyncRegister().execute(); 
       } 
       else 
       { 
        Toast.makeText(getApplicationContext(), "Check you number again", Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 

    } 

    private class AsyncRegister extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 
      t = (EditText) findViewById(R.id.editText1); 
      ServerAPI.registerUser(t.getText().toString()); 
      return "Executed"; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
     } 

     @Override 
     protected void onPreExecute() {} 

     @Override 
     protected void onProgressUpdate(Void... values) {} 
    } 

    private class AsyncReceiveMessage extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 
      ServerAPI.receiveMessage(t.getText().toString()); 
      return "Executed"; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
     } 

     @Override 
     protected void onPreExecute() {} 

     @Override 
     protected void onProgressUpdate(Void... values) {} 
    } 

} 
+0

請不要在這裏傾倒你的整個項目。只發布相關的代碼,並提供堆棧跟蹤。此外,顯示拋出異常的行。 – nhaarman

回答

0

你訪問到主線程中的AsyncTaskdoInBackground方法時:

@Override 
protected String doInBackground(String... params) { 
    ServerAPI.receiveMessage(t.getText().toString()); 
    return "Executed"; 
} 

如果您在doInBackground註釋的第一行代碼你得到的錯誤? 避免這種情況,並嘗試在onPreExecute之前獲取這些值,doInBackgroundparams字符串參數或傳遞給AsyncTask構造函數。

+0

當我評論該行時,它正常工作。該行負責創建發佈請求並從服務器獲取一些數據。 – omarsafwany

+0

那麼,如果你只是在該方法中對EditTextBox的文本進行硬編碼會發生什麼?例如'ServerAPI.receiveMessage(「WriteHereWhatTextBoxHas」);'它工作嗎? – GoRoS

+0

該函數沒有被調用。因此很奇怪 – omarsafwany

相關問題