【发布时间】:2013-04-19 23:32:22
【问题描述】:
您好,我是 android 新手,正在关注一个 yamba 应用程序,该应用程序在 twitter 上发布和获取状态。在添加新部件之前,我工作正常。
我使用不同的线程来执行该帖子。但是一旦我运行该项目,logcat 会说“应用程序可能在主线程上做了太多的工作”。当我输入状态时,“线程退出并出现未捕获的异常”,由于这个 doInBackGround 方法中的 NullPointException。
任何人都可以帮助我吗?我什至注册了一个 Twitter 帐户以确保它不为空。但是非常感谢您的任何回答!
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";
}
}
在yamba应用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;
}
【问题讨论】:
-
我看到你确实使用
synchronized,UI 线程上是否有任何使用这些同步函数的东西? -
在格式修复之前投反对票。您希望尽可能多的人真正阅读此内容,对吗?
标签: android multithreading nullpointerexception