我刚刚开始Android开发,我相信你可以帮忙(对不起我的英文很抱歉)Android SetText空指针异常
我有一个主要活动,并在某个时刻我想打电话给另一个活动,在这个活动中想要用一些消息来改变一个文本视图。在这一刻,我得到一个空指针异常,如果我不把 setContentView(R.layout.register);
但是,当我把这一行,我看到一个毫秒正确的注册活动与我的新文本“Android2”,然后再跳到没有文字的注册活动。我的意思是我画了两次。 我希望我已经解释够了。
问题是我在哪里必须把setcontentview和什么layaout。
非常感谢你,丹尼尔
我告诉你一些代码:
我的主要活动有以下方法:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
Intent i = new Intent(this, register.class);
startActivity(i);
setContentView(R.layout.register);
TextView text = (TextView) findViewById(R.id.texto);
try {
text.setText("Android2");
}catch(Exception e) {
Log.i("Log", e.getMessage()+"Error!"); // LogCat message
}
}
//super.onActivityResult(requestCode, resultCode, data);
}
我的第二个活动类称为寄存器:
public class register extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
}
寄存器接口是register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/register" />
<TextView
android:id="@+id/texto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/continue_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/save" />
<Button
android:id="@+id/new_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/repeat" />
</LinearLayout>
</LinearLayout>
你可以在第一个活动中粘贴你的总代码吗你有没有创建 – 2012-04-22 08:37:50