2017-06-17 5 views
0

私はアンドロイドスタジオが新しく、同じバージョン2.3を使用しています。インテントの概念を学んでいます.ApplesとBananas.EachにはtextViewとButtonがあります。 ApplesはeditText.User入力をeditTextに持ち、button.thenをクリックするとバナナが開き、textViewがに変わります。ボタンクリック後にTextViewが消える

私の問題がある:これはバナナの活動のためのJavaコードに起因するコードの行にあることを発見したボタンをクリックした後、バナナでTextViewにはdisappearing.Iです:

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時間を過ごした...ありがとう:) –

関連する問題