2017-09-22 58 views
-4

我是Android和Java的新手。我試圖捕獲一個活動中的值,並使用一個包將它們傳遞給另一個活動。該包已創建好。但是,當我嘗試提取第二個活動中的值時,我得到空值。我究竟做錯了什麼? 主要活動Android接收捆綁文本返回空

//Create the bundle 
Bundle mybundle = new Bundle(); 
//Add your data to bundle 
mybundle.putString("One",textOne); 
mybundle.putString("Two",textTwo); 
//Add the bundle to the intent 
myIntent.putExtras(mybundle); 
//Fire the second activity 
startActivity(startIntent); 

這是獲取文本的第二個活動

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
Bundle mybundle = getIntent().getExtras(); 
String myText1 = mybundle.getString("One"); 
String myText2 = mybundle.getString("Two"); 

確定的代碼。我將分享我的所有代碼。我正在嘗試一個非常簡單的程序來學習android。在第一個屏幕上輸入兩個數字並加在一起。在第二個屏幕上使用一個包將第一個屏幕的值傳遞給第二個屏幕。

主要活動

public class MainActivity extends AppCompatActivity { 
EditText editNum1; 
EditText editNum2; 
Button addbutton; 
@Override 
protected void onCreate(Bundle savedInstanceState) {   
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
editNum1 = (EditText) findViewById(R.id.editNum1); 
editNum2 = (EditText) findViewById(R.id.editNum2); 
Button addbutton = (Button) findViewById(R.id.button); 
addbutton.setOnClickListener(new View.OnClickListener(){ 
@Override 
public void onClick(View view){ 
Intent myIntent = new Intent(MainActivity.this, 
AddActivity.class); 
final String textOne = editNum1.getText().toString().trim(); 
final String textTwo = editNum2.getText().toString().trim(); 
//Create the bundle 
    Bundle mybundle = new Bundle(); 
//Add your data to bundle 
mybundle.putString("One",textOne); 
mybundle.putString("Two",textTwo); 
//Add the bundle to the intent 
myIntent.putExtras(mybundle); 
//Fire the second activity 
startActivity(myIntent); 

OK現在的AddActivity畫面我已經插入一些調試行的代碼不能工作或者

public class AddActivity extends AppCompatActivity { 
    Number gNum1; 
    Number gNum2; 
    Number total; 
    String myText1; 
    String myText2; 
    TextView textView; 
    TextView textView2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Bundle mybundle = getIntent().getExtras(); 
    if (mybundle != null) { 
     String myText1 = mybundle.getString("One"); 
     String myText2 = mybundle.getString("Two"); 
    } 
    textView = (TextView)findViewById(R.id.textView); 
    if (myText1 != null) { 
     textView.setText(myText1); 
    }else { 
     textView.setText("FAILED"); 
    } 

    textView2 = (TextView)findViewById(R.id.textView2); 
    if (myText1 != null) { 
     textView2.setText(myText1); 
    }else{ 
     textView2.setText("FAILED"); 
+0

您的代碼沒問題。也許textOne或textTwo爲空。你如何得到它? –

+0

可以請你把你的意圖代碼放在這裏。因爲你使用'startExtras(mybundle)'''發送數據'開始意圖'startActivity(startIntent)'。 – UltimateDevil

+1

您正在向myIntent添加綁定,並且您傳遞了不同的意圖(startIntent),請您添加完整的代碼。 – livemaker

回答

0

你加入捆myIntent和你傳遞BundleIntent intent(startIntent)

使用此

//Create the bundle 
Bundle mybundle = new Bundle(); 
//Add your data to bundle 
mybundle.putString("One",textOne); 
mybundle.putString("Two",textTwo); 
//Add the bundle to the intent 
startIntent.putExtras(mybundle); 
//Fire the second activity 
startActivity(startIntent); 

//Create the bundle 
Bundle mybundle = new Bundle(); 
//Add your data to bundle 
mybundle.putString("One",textOne); 
mybundle.putString("Two",textTwo); 
//Add the bundle to the intent 
myIntent.putExtras(mybundle); 
//Fire the second activity 
startActivity(startIntent); 

注意的insted的: 如果從TextviewEditext獲得價值超過

使用本

textOne.getText().toString().trim()

insted的這個

textOne 
+0

我修剪了文本,並確保我在startActivity行中有正確的意圖名稱,但我仍在第二個活動中獲得Null包。讓我分享我的所有代碼 – Pickles57

+0

分享你的所有代碼@ Pickles57 –

+0

@ Pickles57你忘了在你的** AddActivity **中添加** setContentView(R.layout.activity_main); ** –

0

你把捆在myIntent但你開始startIntent您的活動,這就是爲什麼你得到NULL

2

您需要添加包意圖誰開始活動。 請使用下面的代碼

startActivity(myIntent); 
+0

我改變了這一點,仍然得到空值。請參閱上面的完整修訂代碼 – Pickles57

0

在第一個活動而創建的捆綁包添加它,你要開始活動取其意圖。

i。e。在第一個活動

//Create the bundle 
Bundle mybundle = new Bundle(); 

//Add your data to bundle 
mybundle.putString("One",textOne); 
mybundle.putString("Two",textTwo); 

//Add the bundle to the intent 
myIntent.putExtras(mybundle); 

//Fire the second activity with your bundle added intent 
startActivity(myIntent); 

然後在第二個活動作出束空校驗,然後檢索它

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

Bundle mybundle = getIntent().getExtras(); 

if(mybundle!=null){ 
    String myText1 = mybundle.getString("One"); 
    String myText2 = mybundle.getString("Two"); 
} 
0
Bundle mybundle = new Bundle(); 
mybundle.putString("One",textOne); 
mybundle.putString("Two",textTwo); 
myIntent.putExtras(mybundle); 
startActivity(myIntent); 

使用myIntent代替startIntent。由於您向myIntent添加了捆綁軟件,因此您無法訪問由另一個Intent調用的捆綁軟件活動

0

您正在myIntent中添加捆綁軟件並使用startIntent啓動該活動。所以,下面的代碼

startActivity(myIntent); 

使用現在的其他活動檢查是否getIntent不爲空,然後從意向取包。

+0

修正了這個問題。仍然不起作用 – Pickles57

+0

嘗試使用if(null!= getIntent())條件以獲得更好的結果。有時也會通過以這種方式檢查條件來解決nullpointerexception。 – Meet