读书人

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

发布时间: 2012-11-04 10:42:42 作者: rapoo

两个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> 



main.xml
Java code
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     android:background="#ffffff">    <TextView        android:layout_width="fill_parent"        android:layout_height="50dp"        android:text="@string/welcome"        android:textSize="30dp"         android:textColor="#ff0000"        android:gravity="center">            </TextView>        <LinearLayout         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        >                <ImageButton                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/d2"                android:id="@+id/imgbutton"            ></ImageButton>"    </LinearLayout></LinearLayout>


result.xml

Java code
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >             <TextView          android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:id="@+id/result"                  />    </LinearLayout>


[解决办法]
DDMS,贴下LOG
[解决办法]
看下logcat哪里报错应该就明白了
[解决办法]
这么明显的你的Main里面引用的layout1,结果你贴出来的是main
而且木有EditText
[解决办法]
layout1.xml 呢?。。。。再帖下logcat看下,应该是小问题,或许楼主已经解决了
[解决办法]
会不会是类型转换那?
[解决办法]
是不是你的第二个Activity没有在manifest.xml里面注册?

[解决办法]
layout1.xml 呢?
[解决办法]
看这个,
import android.database.CursorJoiner.Result;

引用错了
[解决办法]
import song.com.BmiTest.Result;
你把Result这个Activity子类导成了数据库的Result类了
[解决办法]
Intent intent = new Intent(Main.this,Result.class); 这个Result的问题,没有引用自己的ResultActivity 引用错了

读书人网 >Android

热点推荐