我剛剛開始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