2017-06-17 109 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个小时找到错误...谢谢:) –