2017-06-17 144 views
0

我是新來的android工作室,我使用2.3的相同。我正在學習意圖的概念。我創建了2個活動:蘋果和香蕉。每個都有一個textView和一個按鈕。蘋果在editText中有一個editText.User輸入並點擊按鈕。然後Bananas打開,並且香蕉的textView變爲。TextView按鈕點擊後消失

我的問題是:點擊按鈕後,在TextView的香蕉是disappearing.I發現,這是由於香蕉活動的Java代碼的代碼行:

bananasText.setText(applesMessage); 

但我不知道什麼樣的變化我應該做什麼。以下是兩項活動的代碼。

//code for Apples activity 
package com.awani.intent; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.content.Intent; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.Menu; 
import android.widget.EditText; 



public class Apples extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_apples); 
} 

public void onClick(View view) { 
    //create instance of intent class 
    //this tells the app that the following is the activity which we want to launch on click of the button 
    Intent I= new Intent(this,Bananas.class); 

    //Refer to the input textfield in the activity 
    final EditText applesInput=(EditText)findViewById(R.id.applesInput); 
    //get the inuput from user 
    String userMessage=applesInput.getText().toString(); 
    //pass this info to the net activity which will appear afterr this click 
    I.putExtra("applesmessage",userMessage);//this method takes info from current activity in the form of k ey-value pair 


    //launch the activity 
    startActivity(I); 

} 
} 


//code for activity Bananas 
package com.awani.intent; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 
import android.view.ViewGroup; 
import android.widget.RelativeLayout; 



public class Bananas extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bananas); 

    //we need this class to accept the extra infromation passed to it by some other activity 
    Bundle applesData=getIntent().getExtras();//this extra information is stored in applesData 
    //test if the data is null or there is something(so that to avoid error) 
    if(applesData==null){ 
     return; 
    } 
    //now as error is taken care of,move forward 
    String applesMessage=applesData.getString("applesMessage");//in the variable applesMessage we are storing the string we got from user by passing the key which we passed in putExtra method 
    //refer to the textview in the banana activity 
    final TextView bananasText=(TextView)findViewById(R.id.bananasText); 
    bananasText.setText(applesMessage);//we change the text Bananas to the text which was input by user 

} 

public void onClick(View view) { 
    //create instance of intent class 
    //this tells the app that the following is the activity which we want to launch on click of the button 
    Intent I= new Intent(this,Apples.class); 

    //launch the activity 
    startActivity(I); 
} 
} 

回答

0

香蕉活動更改密鑰來獲取值,因爲在第一項活動您發送的關鍵是「applesmessage」,並在香蕉您收到的「applesMessage」

String applesMessage=applesData.getString("applesMessage"); 

String applesMessage=applesData.getString("applesmessage"); 
+0

廢話!我真的很抱歉,我在這裏提出這樣的疑問......這是愚蠢的錯誤,我花了3個小時找到錯誤...謝謝:) –