照个例子做个简单的程序,一共两个activity,布局也很简单,但始终都调不通。
全部代码如下
main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/tv1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/intro" />
<EditText android:id="@+id/height" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numeric="integer" />
<RadioGroup android:id="@+id/sex" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<RadioButton android:id="@+id/male" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/male" />
<RadioButton android:id="@+id/female"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/female" />
</RadioGroup>
<Button android:id="@+id/submit" android:text="@string/submit"
android:layout_width="50pt" android:layout_height="wrap_content" />
</LinearLayout>result.xml
<?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"> <TextView android:id="@+id/result" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/back" android:layout_width="50pt"
android:layout_height="wrap_content" android:text="@string/back" /></LinearLayout>IntentDemo.javapackage com.android.jiashie;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.content.Intent;public class IntentDemo extends Activity {
private EditText height;
private RadioButton sex;
private Button btn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv1 = (TextView)findViewById(R.id.tv1);
        tv1.setText(R.string.intro);
        height=(EditText)findViewById(R.id.height);
        sex = (RadioButton)findViewById(R.id.sex);
        btn = (Button)findViewById(R.id.submit);
        btn.setText(R.string.submit);
        btn.setOnClickListener(new Button.OnClickListener() {
         public void onClick(View v)
         {
         //TODO:
         }
        });
    }
}ResultActivity.javapackage com.android.jiashie;import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;public class ResultActivity extends Activity
{ private Button btn;

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
btn = (Button)findViewById(R.id.back);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//TODO:
}
});
}
}

解决方案 »

  1.   

    strings.xml
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, IntentDemo!</string>
        <string name="app_name">IntentDemo</string>
        <string name="result">计算结果</string>
        <string name="intro">输入你的身高(cm)</string>
        <string name="male">男</string>
        <string name="female">女</string>
        <string name="submit">提交</string>
        <string name="back">返回</string>
    </resources>
    androidmanifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.android.jiashie"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="4" />    <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".IntentDemo"
                      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=".ResultActivity"
    android:label="@string/result">
    </activity>
        </application>
    </manifest>
      

  2.   

    在模拟器上运行就出现
    the application xxx (...) has stopped unexpectedly
    的经典错误。
    怀疑与layout的xml文件有关(因为在未添加resultActivity之前,也出现这个错误,然后修改了main.xml后,就正常了,然后添加了result.xml和resultActivity,又出现这个错误)
    好像在布局时各控件的位置关系没摆对也会出错?
      

  3.   

    你sex = (RadioButton)findViewById(R.id.sex);这里的代码不对。sex应该属于RadioGroup的
      

  4.   

    sex = (RadioButton)findViewById(R.id.sex);
    ------------------------
    找到原因了sex为RadioButton
    但R.id.sex指向的是一个RadioGroup郁闷的是,IDE中竟然没有报错。----------------------
    来几个人接分啊,不然又被怀疑倒分了。
    此回复无分。
      

  5.   

    其他的问题没发现,可以正常开启你的resultactivity
      

  6.   

    每次解决完自己的 the application xxx (...) has stopped unexpectedly 错误,
    自己都会觉得之前好可笑 
     
      

  7.   


    这种情况下IDE是不会报错的吧..因为R.id.sex不过是int型, 不会判断它是哪种控件啊, 从语法上说是没有错误的
      

  8.   


    我也出了这个错误,在log下还看不懂,原来是自己不小心写错了