从另一个 Activity 发送意图时无法更改文本视图的文本
Not able to change text of textview while sending intent from another Activity
我试了很多才给大家添麻烦了。
我正在 开发聊天应用程序,每个 Activity..
都有服务器连接
Toast 在Messege 上工作得很好Activity 所以发送Intent 没有问题。
但是当我尝试更新 UI 时它给了我错误。
我正在发送这样的意图
intentt.putExtra("name", values[arg2]);
并获得如下意图
Bundle extras = this.getIntent().getExtras();
String id = extras.getString("name");
我在 TextView
中设置 id 的代码
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.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)
如果您在 activity,只需将代码更改为此即可。它会告诉你你的问题到底是什么。
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();
}
我试了很多才给大家添麻烦了。 我正在 开发聊天应用程序,每个 Activity..
都有服务器连接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.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)
如果您在 activity,只需将代码更改为此即可。它会告诉你你的问题到底是什么。
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();
}