0

Hello I am new to android and is following a yamba application which posts and get status from the twitter. I was working fine before I added the new part.

I used a different thread to perform the post. But once I run the project, logcat says"application may be doing too much work on main thread".When I type in status,"thread exiting with uncaught exception",due to NullPointException in this doInBackGround method.

Anyone can help me??I have even registered a Twitter account to make sure it is not null.But Thanks alot for any answer!!

    public class MainActivity extends Activity implements OnClickListener,TextWatcher
    {
    private static final String TAG="StatusActivity";
    EditText editText;
    Button buttonUpdate;
    TextView textCount;
    private YambaApplication yamba;


//constructor
     @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

editText=(EditText) findViewById(R.id.editText);
buttonUpdate=(Button) findViewById(R.id.buttonUpdate);
textCount=(TextView) findViewById(R.id.textCount);
textCount.setText(Integer.toString(140));
textCount.setTextColor(Color.GREEN);

buttonUpdate.setOnClickListener(this);
editText.addTextChangedListener(this);
this.yamba=(YambaApplication)getApplication();
}   


    //Main task of posting, three method of AsyncTask
class PostToTwitter extends AsyncTask<String,Integer,String>
{
    @Override
    protected String doInBackground(String...statuses)
    {
        try{
             Twitter.Status status=yamba.getTwitter().updateStatus(statuses[0]);
             return status.text;
        }
        catch(TwitterException e){
            Log.e(TAG, e.toString());
            e.printStackTrace();
            return "Failed to post";
        }
    }

There is this task in the yamba application java:

    public synchronized Twitter getTwitter(){
    if(twitter==null)
    {
        String username,password,APIroot;
        username=prefs.getString("username", "");
        password=prefs.getString("passord", "");
        APIroot=prefs.getString("APIroot", "http://yamba.marakana.com/api");

     if ( !TextUtils.isEmpty(username) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(APIroot) )
         {
            twitter= new Twitter(username,password);
             twitter.setAPIRootUrl(APIroot);
          }
     }
    return this.twitter;
  } 
user2295285
  • 11
  • 1
  • 1

1 Answers1

0

Don't do any blocking operations in the main loop that also serves the UI. This includes things like making requests to a remote system, twitter and yamba in this case. While it seems to have been moved to a different thread, you are still blocking the yamba interface while making a request there, which blocks it for other uses, too.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • Thank you for replying. Now I look at the logcat again, "äpplication may be doing to much work on main thread" right after the main activity is created, before I type in status or do any movement. But the onCreate() of both the whole application and the main activity only inflated the R view...does it mean that the synchronized task is started automatically? if synchronized task will still block the main thread, but i do need the task to report back,then what should i use instead? what is unclear about the formatting? – user2295285 Apr 20 '13 at 11:51
  • Start off with indenting the code consistently. What is a "logcat"? That said, maybe you shouldn't take the error message verbatim, i.e. that the main thread is overloaded, but rather that whatever it does takes too long. This includes waiting for other threads. – Ulrich Eckhardt Apr 20 '13 at 14:02