【求助】the application xxx (...) has stopped unexpectedly...

whatafuckinglongname 2011-05-13 04:42:27
照个例子做个简单的程序,一共两个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.java

package 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.java

package 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:
}
});
}
}

...全文
249 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
KG071 2011-06-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lyl0426 的回复:]
引用 5 楼 jiashie 的回复:

sex为RadioButton
但R.id.sex指向的是一个RadioGroup

郁闷的是,IDE中竟然没有报错。



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

我也出了这个错误,在log下还看不懂,原来是自己不小心写错了
lyl0426 2011-05-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jiashie 的回复:]

sex为RadioButton
但R.id.sex指向的是一个RadioGroup

郁闷的是,IDE中竟然没有报错。

[/Quote]

这种情况下IDE是不会报错的吧..因为R.id.sex不过是int型, 不会判断它是哪种控件啊, 从语法上说是没有错误的
念茜 2011-05-13
  • 打赏
  • 举报
回复
每次解决完自己的 the application xxx (...) has stopped unexpectedly 错误,
自己都会觉得之前好可笑
easycoola 2011-05-13
  • 打赏
  • 举报
回复
其他的问题没发现,可以正常开启你的resultactivity
jiashie 2011-05-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 easycoola 的回复:]
你sex = (RadioButton)findViewById(R.id.sex);这里的代码不对。sex应该属于RadioGroup的
[/Quote]
自己也刚发现了。。
jiashie 2011-05-13
  • 打赏
  • 举报
回复
sex = (RadioButton)findViewById(R.id.sex);
------------------------
找到原因了

sex为RadioButton
但R.id.sex指向的是一个RadioGroup

郁闷的是,IDE中竟然没有报错。

----------------------
来几个人接分啊,不然又被怀疑倒分了。
此回复无分。
easycoola 2011-05-13
  • 打赏
  • 举报
回复
你sex = (RadioButton)findViewById(R.id.sex);这里的代码不对。sex应该属于RadioGroup的
  • 打赏
  • 举报
回复
这个算是初学者的经典问题不?
  • 打赏
  • 举报
回复
在模拟器上运行就出现
the application xxx (...) has stopped unexpectedly
的经典错误。
怀疑与layout的xml文件有关(因为在未添加resultActivity之前,也出现这个错误,然后修改了main.xml后,就正常了,然后添加了result.xml和resultActivity,又出现这个错误)
好像在布局时各控件的位置关系没摆对也会出错?
  • 打赏
  • 举报
回复
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>

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧