2012-04-22 132 views
2

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

你可以在第一個活動中粘貼你的總代碼嗎你有沒有創建 – 2012-04-22 08:37:50

回答

1

你有兩個不同的活動。其中之一是使用register.xml視圖,第二個是試圖訪問註冊視圖。該視圖僅存在於您的"register"活動中。其他活動似乎沒有看法?這可能是你得到NULL的原因。

您應該將這兩個類合併在一起,因爲它看起來像您試圖從相同視圖訪問texto

因此,總而言之,findViewById應該從調用setContentView的活動中調用。

+0

非常感謝你的幫助。我真的很喜歡它。丹尼爾。 – daniel 2012-04-22 19:25:49

0

刪除此行,它應該工作

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 
      } 
    } 
+0

太棒了!它的工作原理,現在我想知道我是否將這段代碼移動到其他活動中,我如何傳遞消息的價值,現在是我在主要類中只知道的測試文本「Android2」?謝謝丹尼爾。 – daniel 2012-04-22 19:20:02

+0

哦,我不得不感謝你,所以'setContentView'必須高於一切幾乎 – Master345 2014-04-18 15:13:04

4

什麼你基本上做的是以下幾點:

  1. 您準備的意圖,開始另一個活動
  2. 啓動活動你準備意圖爲
  3. 您將當前活動的內容設置爲R.layout。註冊
  4. 你得到的TextView(在當前活動)
  5. 你的TextView的文本設置爲Android2

而且,此時的新的活動出現在屏幕上。請注意,您的代碼不正確,因爲您操作當前活動中的UI元素,並且您希望對新開始的活動進行更改。

移動將此代碼

TextView text = (TextView) findViewById(R.id.texto); 

try { 
    text.setText("Android2"); 
}catch(Exception e) { 
    Log.i("Log", e.getMessage()+"Error!"); // LogCat message 
} 

到寄存器活動的onCreate()方法。

順便說一句,通常,當你創建一個類時,它的名字應該根據標準以大寫字母開頭。

+1

非常感謝你的幫助。我真的很喜歡它。丹尼爾。 – daniel 2012-04-22 19:26:22

+0

你能把我的答案投票嗎?謝謝 – overbet13 2012-04-22 19:47:51

+0

如果您認爲這是答案,請檢查此答案。謝謝 – overbet13 2012-04-23 13:07:21