按下按钮时强制应用程序关闭错误

时间:2012-04-07 18:29:44

标签: java android if-statement android-edittext

因此,每当我点击启动页面上的按钮时,它都会给我一个强制关闭错误。这是main.xml布局文件的类:

public class ForeverAloneActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btnstrt = (Button) findViewById(R.id.toq1);
        btnstrt.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent frstq = new Intent(v.getContext(), QuestionOne.class);
                startActivityForResult(frstq, 0);

这就是我认为产生错误的原因。此类与启动页面上按下按钮时的页面相关:

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView (R.layout.frstq);
    Button startQ2 = (Button) findViewById(R.id.toq2);
    startQ2.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent toQ2 = new Intent(v.getContext(), QuestionTwo.class);
            final EditText number = (EditText) findViewById(R.id.editText1);
            final Toast error = Toast.makeText(QuestionOne.this, "Please insert a value", Toast.LENGTH_SHORT);
            if (number.getText().toString().equals("")) {error.show();
            }else{
            startActivityForResult(toQ2, 0);}

if语句就像下一页那样,有一个EditText框。我试图这样做,以便如果EditText框中没有任何内容,它会显示一个toast消息,说"请插入一个值"。直到将一个整数放入EditText框中,该按钮才会起作用。

如果有人可以提供帮助,我们将不胜感激。

Logcat:

04-07 19:33:58.199: W/dalvikvm(362): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-07 19:33:58.221: E/AndroidRuntime(362): FATAL EXCEPTION: main
04-07 19:33:58.221: E/AndroidRuntime(362): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.kenning.foreveralone/com.kenning.foreveralone.QuestionOne}; have you declared this activity in your AndroidManifest.xml?
04-07 19:33:58.221: E/AndroidRuntime(362):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
04-07 19:33:58.221: E/AndroidRuntime(362):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
04-07 19:33:58.221: E/AndroidRuntime(362):  at android.app.Activity.startActivityForResult(Activity.java:2817)
04-07 19:33:58.221: E/AndroidRuntime(362):  at com.kenning.foreveralone.ForeverAloneActivity$1.onClick(ForeverAloneActivity.java:22)
04-07 19:33:58.221: E/AndroidRuntime(362):  at android.view.View.performClick(View.java:2408)
04-07 19:33:58.221: E/AndroidRuntime(362):  at android.view.View$PerformClick.run(View.java:8816)
04-07 19:33:58.221: E/AndroidRuntime(362):  at android.os.Handler.handleCallback(Handler.java:587)
04-07 19:33:58.221: E/AndroidRuntime(362):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 19:33:58.221: E/AndroidRuntime(362):  at android.os.Looper.loop(Looper.java:123)
04-07 19:33:58.221: E/AndroidRuntime(362):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-07 19:33:58.221: E/AndroidRuntime(362):  at java.lang.reflect.Method.invokeNative(Native Method)
04-07 19:33:58.221: E/AndroidRuntime(362):  at java.lang.reflect.Method.invoke(Method.java:521)
04-07 19:33:58.221: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-07 19:33:58.221: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-07 19:33:58.221: E/AndroidRuntime(362):  at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

你在清单文件中添加QuestionTwo吗? 检查错误 - 您是否在AndroidManifest.xml中声明了此活动?

答案 1 :(得分:0)

在清单文件中声明您的活动。你似乎忘了那个

<activity android:name="QuestionTwo" />

也不要忘记在清单文件中包含每个活动

答案 2 :(得分:0)

嘿,你已将QuestionTwo类添加为menifest文件中的一个活动。如果添加

之后
Intent toQ2 = new Intent(YourCurrentActivity.this,QuestionTwo.class);

然后你必须没有在menifest.B'coz中添加活动它在日志中清楚地显示未找到Activity。您必须在清单文件中声明活动。

希望你能运行你的应用程序。