2017-08-12 21 views
-1

SplashActivityという名前のクラスを1つ作成しました。このアクティビティに2つの数値を追加しました。次のアクティビティで回答を表示しますが、 2つのEditTextフィールドと1つのボタンを作成しました。ボタンをクリックすると、新しいアクティビティが開始され、その追加の回答が表示されます。androidの1つのアクティビティから別のアクティビティへの加算結果を渡す方法

EditText e=(EditText)findViewById(R.id.editText3); 
    EditText e2=(EditText)findViewById(R.id.editText4);` 
    int num1=Integer.parseInt(e.getText().toString()); 
    int num2=Integer.parseInt(e2.getText().toString()); 
    int sum=num1+num2; 
    Intent in=new Intent(this,SecondSplash.class); 
    in.putExtra("value",sum); 
    startActivity(in); 

とそのXMLは

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.sammar.firstapplication.SplashActivity" 
    android:background="@drawable/background"> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="76dp" 
     android:ems="10" 
     android:inputType="numberSigned" 
     android:hint="Enter First Number"/> 

    <EditText 
     android:id="@+id/editText4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="24dp" 
     android:ems="10" 
     android:inputType="numberSigned" 
     android:hint="Enter Second Number" 
     android:layout_below="@+id/editText3" 
     android:layout_alignStart="@+id/editText3" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginStart="46dp" 
     android:onClick="onButtonClick" 
     android:text="ADD" 
     android:layout_above="@+id/button6" 
     android:layout_alignParentStart="true" 
     android:layout_marginBottom="51dp" /> 

であり、これはSecondActivityのコードです。このアクティビティでは、データを受け取るバンドルを作成しました。

TextView txtView; 
    String value; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second_splash); 

     Bundle b=getIntent().getExtras(); 
     if(b!=null) 
     { 
      value=b.getString("value"); 
     } 
     txtView =(TextView)findViewById(R.id.textView2); 
     //value=getIntent().getExtras().getString("value"); 
     txtView.setText(value); 

とそのXMLは、私は2番目の活動に和の値を取得することはできません

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.sammar.firstapplication.SecondSplash"> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginStart="132dp" 
     android:layout_marginTop="191dp" 
     /> 
</RelativeLayout> 

です。親切に助けてくださいenter code here

答えて

0
value=b.getInt("value"); 

合計のタイプはint型ない文字列です。

+0

私もStringにfristactivityに合計を解析する必要がやりたいのonClick中にあなたの意図、

button.setOnClickListner(new OnClickListner) { @override OnClick() { Intent in=new Intent(this,SecondSplash.class); in.putExtra("value",String.valueOf(sum)); startActivity(in); } } 

変更してコード

Bundle b=getIntent().getExtras(); if(b!=null) { value=b.getString("value"); } 

を置きますか。? –

+0

txtView.setText(value);文字列を受け取ります –

0

String value = getIntent().getStringExtra("value"); 
+0

コードを変更しましたが、それでもまだ仕事がありません –

+0

編集したans ... in.putExtra()行をチェックしてください –

関連する問題