在给大家惹麻烦之前,我做了很多尝试。 我正在研究聊天应用程序,因为每个活动都有一个连接点。
Toast在Messege Activity上工作得非常好,因此发送Intent没有问题。 但是当我尝试更新UI时,它会给我错误。
我发送这样的意图
intentt.putExtra("name", values[arg2]);
并获得如下的意图
Bundle extras = this.getIntent().getExtras();
String id = extras.getString("name");
我在TextView
else if (id != null) {
// tx.setText(id);
Thread thread = new Thread() {
public void run() {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(Messaging.this,
"Something happened.", Toast.LENGTH_SHORT)
.show();
tx.setText(id);
}
});
};
};
thread.start();
}
我在下面提到错误..
01-14 12:17:00.469: E/AndroidRuntime(5207): FATAL EXCEPTION: main
01-14 12:17:00.469: E/AndroidRuntime(5207): java.lang.NullPointerException
01-14 12:17:00.469: E/AndroidRuntime(5207): at at.vcity.androidim.Messaging$2$1.run(Messaging.java:122)
01-14 12:17:00.469: E/AndroidRuntime(5207): at android.os.Handler.handleCallback(Handler.java:615)
01-14 12:17:00.469: E/AndroidRuntime(5207): at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 12:17:00.469: E/AndroidRuntime(5207): at android.os.Looper.loop(Looper.java:137)
01-14 12:17:00.469: E/AndroidRuntime(5207): at android.app.ActivityThread.main(ActivityThread.java:4812)
01-14 12:17:00.469: E/AndroidRuntime(5207): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 12:17:00.469: E/AndroidRuntime(5207): at java.lang.reflect.Method.invoke(Method.java:511)
01-14 12:17:00.469: E/AndroidRuntime(5207): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-14 12:17:00.469: E/AndroidRuntime(5207): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
01-14 12:17:00.469: E/AndroidRuntime(5207): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
如果您正在进行活动,只需将代码更改为此代码。它会告诉你究竟是什么问题。
else if (id != null) {
// tx.setText(id);
Thread thread = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(Messaging.this,
"Something happened.", Toast.LENGTH_SHORT)
.show();
if(tx!=null)
{
if(id!=null && id.length()>0)
{
tx.setText(id);
}
else
{
Toast.makeText(Messaging.this,"id is null or is empty", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(Messaging.this,"Tx is null", Toast.LENGTH_SHORT).show();
}
}
});
};
};
thread.start();
}