Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击按钮后文本视图消失_Java_Android - Fatal编程技术网

Java 单击按钮后文本视图消失

Java 单击按钮后文本视图消失,java,android,Java,Android,我是android studio的新手,我正在使用相同版本的2.3。我正在学习意图的概念。我创建了两个活动:苹果和香蕉。每个活动都有一个文本视图和一个按钮。苹果有一个editText。用户在editText中输入并单击按钮。然后香蕉打开,香蕉的文本视图更改为 我的问题是:单击按钮后,香蕉中的textView正在消失。我发现这是由于香蕉活动java代码中的一行代码造成的: bananasText.setText(applesMessage); 但我不知道我应该做什么改变。 下面是这两个活动的代码

我是android studio的新手,我正在使用相同版本的2.3。我正在学习意图的概念。我创建了两个活动:苹果和香蕉。每个活动都有一个文本视图和一个按钮。苹果有一个editText。用户在editText中输入并单击按钮。然后香蕉打开,香蕉的文本视图更改为

我的问题是:单击按钮后,香蕉中的textView正在消失。我发现这是由于香蕉活动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);
}
}

在香蕉活动中,更改键以获取值,因为在第一个活动中,您以“applesmessage”的形式发送键,在香蕉活动中,您以“applesmessage”的形式接收键


废话!我真的很抱歉,我在这里提出了这样一个疑问…这是一个愚蠢的错误,我花了大约3个小时来寻找错误…谢谢:)
String applesMessage=applesData.getString("applesMessage");
String applesMessage=applesData.getString("applesmessage");