2014-08-30 50 views
-4
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity1); 
     Button btn = (Button) findViewById(R.id.btn); 
     btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      TextView tView = (TextView) findViewById(R.id.tView); 
      String myText = "this is the text"; 
      // note this textview is in the Newactivity layout. A null pointer exception is thrown here 
      tView.setText(myText); 
      //this moves theactivity1 intent to the NewActivity intent. It works   
      Intent newActIntent = new Intent(view.getContext(),NewActivity.class); 
      startActivity(newActIntent); 

     } 
    }); 
} 
+0

只需通過您的意圖傳遞額外值,然後在第二個活動中提取它並設置文本視圖的值 – Eenvincible 2014-08-30 23:37:00

+0

請編輯您的代碼以使其可讀。格式化您的代碼的縮進。 – afzalex 2014-08-30 23:39:51

回答

-1

你應該在你的第一個活動是利用

newActIntent.putExtra("someKey", myText);傳遞字符串值NewActivity。

你的onClick方法應該是這樣的:

public void onClick(View view) { 

     String myText = "this is the text"; 
     Intent newActIntent = new Intent(view.getContext(),NewActivity.class); 
     newActIntent.putExtra("someKey", myText); 
     startActivity(newActIntent); 
} 

然後在你的NewActivity時應提取存儲在第一活動someKey值,並使用提取的值設置TextView的文本:

String textValue = intent.getExtras().getString("someKey"); 
TextView tView = (TextView) findViewById(R.id.tView); 
tView.setText(textValue); 

當您嘗試在第一個活動中設置文本時,您收到NullPointerException的原因是findViewById(id)將嘗試在當前A的佈局中查找具有指定標識的視圖活動,而不是每個活動。出於這個原因,您應該首先啓動聲明TextView的Activity,然後使用findViewById(id)方法檢索TextView,然後根據您使用Intent傳遞的值設置文本。

+0

Thanks.i現在已經清楚了。但我在String上接收到空指針textValue = intent.getExtras()。getString(「name」); – 2014-08-31 00:13:13

+0

我假設你正在使用getIntent()方法將意圖保存到變量中。在你的onCreate方法中,你應該有類似Intent intent = getIntent()的東西,然後調用getExtras() – LuisF 2014-08-31 00:21:31

+0

你能解釋-1嗎?我的回答與所選答案相同...... – LuisF 2014-08-31 01:28:54

-1

如果您尚未設置其先前所屬的佈局,則無法訪問視圖。您將在創建NewActivity時設置textview的內容。

爲了達到這個目的,您將爲啓動新活動的意圖賦予一個額外字符串參數。

public class YourActivity extends Activity { 

    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.your_layout); // this has to be called before accessing views 
    Button btn = (Button) findViewById(R.id.btn); 

    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
     Intent newActIntent = new Intent(view.getContext(),NewActivity.class); 
     String myText = "this is the text"; 
     newActIntent.putExtra("TextViewContent",mText); 
     startActivity(newActIntent); 
     } 
    });   
    } 

} 

public class NewActivity extends Activity { 

    protected void onCreate(Bundle savedInstanteState) { 
    super.onCreate(savedInstanceState); 
    Intent intent = getIntent(); 
    String stringContent = intent.getStringExtra("TextViewContent"); 
    setContentView(R.layout.your_new_activity); 
    TextView textview = (TextView) findViewById(R.id.tView); 
    textview.setText(stringContent); 

    } 

} 
-1

您正在使用的代碼是錯誤的,因爲你不能使用findViewById得到一種觀點認爲,是不是在當前佈局。

你可以做到以下幾點:

  • 在第一個活動onClick方法中,在創建意圖將文本要在一個額外的:

    newActIntent.putExtra("name", "some text");

  • 在OnCreate第二次活動的方法:

    TextView tView = (TextView) findViewById(R.id.tView); String myText= getIntent().getExtras().getString("name"); tView.setText(myText);

編輯

你有2個活動:

活動A我想你已經發布對應於該活動的代碼。這將是:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity1); 
    Button btn = (Button) findViewById(R.id.btn); 
    btn.setOnClickListener(new View.OnClickListener() { 
    @Override 
     public void onClick(View view) { 
      // TextView tView = (TextView) findViewById(R.id.tView); 


      Intent newActIntent = new Intent(view.getContext(),NewActivity.class); 
      String myText = "this is the text"; 
      newActIntent.putExtra("name", myText); // Adding the extra 
      startActivity(newActIntent); 

     } 
    }); 
} 

活動B

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity2); 

    TextView tView = (TextView) findViewById(R.id.tView); 
    String myText = getIntent().getExtras().getString("name"); // Retrieving the extra 
    tView.setText(myText); 

    //... 
} 
+0

謝謝。但你能否請進一步解釋? – 2014-08-30 23:55:29

+0

感謝,它現在工作。 – 2014-08-31 00:34:04

+0

我添加了另一個按鈕,第一個意圖在同一頁面上顯示不同的文本。取決於哪個按鈕被點擊,但是當我按下它時第一個按鈕沒有顯示文本。只是secon正在工作。你能幫助我嗎? – 2014-08-31 00:46:59

相關問題