当前位置: 代码迷 >> Android >> 两个Activity其间传数据,点击按钮意外终止
  详细解决方案

两个Activity其间传数据,点击按钮意外终止

热度:86   发布时间:2016-05-01 13:01:10.0
两个Activity之间传数据,点击按钮意外终止
代码如下,麻烦高手指点下,搞了大半天了,没个头绪。
Main.java
Java code
package song.com.BmiTest;import android.app.Activity;import android.content.Intent;import android.database.CursorJoiner.Result;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;public class Main extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.layout1);                Button bn =(Button)findViewById(R.id.bn);        bn.setOnClickListener(new Button.OnClickListener(){            @Override            public void onClick(View v) {                EditText wt =(EditText)findViewById(R.id.weight);                EditText ht =(EditText)findViewById(R.id.height);                double height=Double.parseDouble(ht.getText().toString());                double weight=Double.parseDouble(wt.getText().toString());                RadioButton male =(RadioButton)findViewById(R.id.male);                String gender=male.isSelected() ?"boy":"girl";                                Intent intent = new Intent(Main.this,Result.class);                Bundle data = new Bundle();                data.putDouble("height", height);                data.putDouble("weight", weight);                data.putString("gender", gender);                                intent.putExtras(data);                startActivity(intent);                            }                                });    }}


Result.java
Java code
package song.com.BmiTest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class Result extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.result);        TextView result =(TextView)findViewById(R.id.result);        Intent intent =getIntent();        Bundle data =intent.getExtras();        String gender =data.getString("gender");        double height =data.getDouble("height");        double weight =data.getDouble("weight");        result.setText("您的性别是"+gender+"您的体重是"+weight+"kg"+","+"身高是"+height+"m");            }    }



AndroidManifest.xml
Java code
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="song.com.BmiTest"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="10" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".Main"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity             android:name=".Result"            android:label="@string/result_name"            ></activity>    </application></manifest>
  相关解决方案