0

因此,每当我单击启动页面上的按钮时,都会出现强制关闭错误。这是 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 框之前,该按钮将不起作用。

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

日志猫:

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)
4

3 回答 3

1

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

于 2012-04-07T18:32:02.787 回答
0

在清单文件中声明您的活动。看来你忘记了

<activity android:name="QuestionTwo" />

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

于 2012-04-07T18:39:24.570 回答
0

嘿,您是否已将 QuestionTwo 类添加为清单文件中的活动。如果添加后

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

那么你一定没有在menifest.B'coz中添加活动,因为它在日志中清楚地显示找不到活动。您必须在清单文件中声明活动。

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

于 2012-04-07T18:41:34.067 回答