Java 使用TwitterAPI创建android应用程序时,控件不会进入线程
代码如下:Java 使用TwitterAPI创建android应用程序时,控件不会进入线程,java,android,multithreading,twitter,Java,Android,Multithreading,Twitter,代码如下: public void onClick(View v) { Log.d(TAG, "hay i am working till here"); new Thread() { public void run() { try { Log.d(TAG, "hay i reached till here"); twitter = new Twitter("student
public void onClick(View v) {
Log.d(TAG, "hay i am working till here");
new Thread() {
public void run() {
try {
Log.d(TAG, "hay i reached till here");
twitter = new Twitter("student", "password");
twitter.setAPIRootUrl("http://yamba.marakana.com/api");
twitter.setStatus(editText.getText().toString());
Log.d(TAG,"Successfully Posted");
} catch (TwitterException e) {
Log.e(TAG, "Died", e);
}
}
}.start();
Log.d(TAG, "onClicked");
}
当我运行它时,它只到达第一个日志,即
Log.d(标记为“我一直工作到这里”)代码>我无法找出问题所在。问题在于,您试图通过代码中包含此行的非ui线程访问ui线程
twitter.setStatus(editText.getText().toString());
应在ui线程上访问/调用Asedittext.getText()
相反,在ui线程上使用处理程序来完成您的需求
Handler tweethandler=new Handler(getMainLooper());
tweethandler.post(新的Runnable(){
公开募捐{
但是你的问题是什么?很抱歉,我通过手机发布了这么糟糕的格式。尝试过这个,但这个错误似乎要了我的命。让我发布整个事情,我之前在ui线程上尝试过这个…那么现在发生了什么?句柄前面有一个错误,即使导入了无法运行的包,你能告诉我必须导入吗为了使用Handler,请输入一些额外的内容将Handler.post替换为tweethandler.post
try {
Log.d(TAG, "hay i reached till here");
twitter = new Twitter("student", "password");
twitter.setAPIRootUrl("http://yamba.marakana.com/api");
twitter.setStatus(editText.getText().toString());
Log.d(TAG,"Successfully Posted");
} catch (TwitterException e) {
Log.e(TAG, "Died", e);
}
}
});
public void onClick(View v) {
Log.d(TAG, "hay i am not working");
Handler.post(new Runnable() {
public void run() {
try {
Log.d(TAG, "hay i reached till here");
twitter = new Twitter("student", "password");
twitter.setAPIRootUrl("http://yamba.marakana.com/api");
twitter.setStatus(editText.getText().toString());
Log.d(TAG, "Successfully Posted");
} catch (TwitterException e) {
Log.e(TAG, "Died", e);
}
}
});
Log.d(TAG, "onClicked");
}