0

这是代码。它不会在控制台中给出任何错误。我制作了另一个活动类 test_me.java。当我单击对话框选项时,它会给出“不幸的是应用程序停止工作”的错误。请告诉我这里有什么问题。

 public class TestActivity extends Activity {

    Context context=this;
    Intent testIntent;
        Intent progIntent;
        Intent settingIntent;
        Intent resultIntent; public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);  
            Button b1 = (Button)findViewById(R.id.button1);        
            b1.setOnClickListener(new Button.OnClickListener(){

        @Override
         public void onClick(View v) {
                  if(v.getId()==R.id.button1)
                  {                   final CharSequence[] items = {"Test", "ProgMode", "Settings","Result"};
                     AlertDialog.Builder builder = new AlertDialog.Builder(context)
                    .setTitle("Menu Options")
                  .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int item) {

                             if(items[item]=="Test")
                            {testIntent = new Intent(TestActivity.this, test_me.class);
                                startActivity(testIntent);
                                }
                             else if(items[item]=="ProgMode")
                                {progIntent = new Intent(TestActivity.this,prog_mode.class);
                                startActivity(progIntent);

                                    }
                             else if(items[item]=="Settings")
                                {settingIntent = new Intent(TestActivity.this,setting_mode.class);
                                startActivity(settingIntent);
                                                                }
                             else if(items[item]=="Results")
                                {
                                 resultIntent = new Intent(TestActivity.this,result_mode.class);
                                startActivity(resultIntent);
                                                                }

                             Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                         }
                     });
                      builder.setPositiveButton("Back",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();

                }
            });
}

这是日志

06-07 17:44:15.598: I/dalvikvm(654): threadid=3: reacting to signal 3
06-07 17:44:15.978: I/dalvikvm(654): Wrote stack traces to '/data/anr/traces.txt'
06-07 17:44:16.088: I/dalvikvm(654): threadid=3: reacting to signal 3
06-07 17:44:16.148: I/dalvikvm(654): Wrote stack traces to '/data/anr/traces.txt'
06-07 17:44:16.538: D/android.widget.GridLayout(654): vertical constraints: y3 - y0 > 637, y1 - y0 > 129, y2 - y1 > 135, y3 - y0 < 618 are inconsistent; permanently removing: y3 - y0 < 618. 
06-07 17:44:16.608: I/dalvikvm(654): threadid=3: reacting to signal 3
06-07 17:44:16.638: I/dalvikvm(654): Wrote stack traces to '/data/anr/traces.txt'
06-07 17:44:16.858: D/gralloc_goldfish(654): Emulator without GPU emulation detected.
06-07 17:44:17.108: I/dalvikvm(654): threadid=3: reacting to signal 3
06-07 17:44:17.139: I/dalvikvm(654): Wrote stack traces to '/data/anr/traces.txt'
06-07 17:44:21.638: D/AndroidRuntime(654): Shutting down VM
06-07 17:44:21.638: W/dalvikvm(654): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-07 17:44:21.688: E/AndroidRuntime(654): FATAL EXCEPTION: main
06-07 17:44:21.688: E/AndroidRuntime(654): android.content.ActivityNotFoundException: Unable to find explicit activity class {achira.test/achira.test.test_me}; have you declared this activity in your AndroidManifest.xml?
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.app.Activity.startActivityForResult(Activity.java:3190)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.app.Activity.startActivity(Activity.java:3297)
06-07 17:44:21.688: E/AndroidRuntime(654):  at achira.test.AchActivity$1$1.onClick(AchActivity.java:84)
06-07 17:44:21.688: E/AndroidRuntime(654):  at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.widget.AdapterView.performItemClick(AdapterView.java:292)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.widget.AbsListView$1.run(AbsListView.java:3168)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.os.Handler.handleCallback(Handler.java:605)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.os.Looper.loop(Looper.java:137)
06-07 17:44:21.688: E/AndroidRuntime(654):  at android.app.ActivityThread.main(ActivityThread.java:4424)
06-07 17:44:21.688: E/AndroidRuntime(654):  at java.lang.reflect.Method.invokeNative(Native Method)
06-07 17:44:21.688: E/AndroidRuntime(654):  at java.lang.reflect.Method.invoke(Method.java:511)
06-07 17:44:21.688: E/AndroidRuntime(654):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-07 17:44:21.688: E/AndroidRuntime(654):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-07 17:44:21.688: E/AndroidRuntime(654):  at dalvik.system.NativeStart.main(Native Method)
06-07 17:44:22.349: I/dalvikvm(654): threadid=3: reacting to signal 3
06-07 17:44:22.388: I/dalvikvm(654): Wrote stack traces to '/data/anr/traces.txt'
06-07 17:44:24.118: I/Process(654): Sending signal. PID: 654 SIG: 9
4

6 回答 6

3

我认为这

Context context=TestActivity.this;

应该进入 onCreate

也关闭你的对话框

重置您的 adb 或重新启动您的模拟器,您将在 logcat 中收到错误

你的日志猫说

E/AndroidRuntime(654): android.content.ActivityNotFoundException: Unable to find explicit activity class {achira.test/achira.test.test_me}; have you declared this activity in your AndroidManifest.xml?

这意味着您尚未在清单中声明您的活动

在应用程序标签中声明这样的活动

<activity
            android:name=".test_me"
            android:label="@string/app_name">
        </activity>
于 2012-06-07T10:40:41.993 回答
0

我有一些类似问题的经验。如果您正在创建上下文变量,请在任何地方使用它。即使是敬酒也不要写 getApplicationContext() 。并在 oncreate(){} 中像这样更改上下文变量

context = TestActivity.this; //pass activity context.

将上下文声明为类级别变量

于 2012-06-07T10:42:32.210 回答
0

按下 singleChoiceButton 后,您不会关闭对话框。dialog.cancel()在方法内部的每个startActivity()语句之后调用onClick

于 2012-06-07T10:42:59.850 回答
0
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();

只需评论或删除此声明...

于 2012-06-07T10:49:07.433 回答
0

在您的代码中进行 2 处更改。

Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();

改成

Toast.makeText(TestActivity .this, items[item], Toast.LENGTH_SHORT).show();

AlertDialog.Builder builder = new AlertDialog.Builder(context)

改成

AlertDialog.Builder builder = new AlertDialog.Builder(TestActivity .this)
于 2012-06-07T11:19:14.737 回答
0

将类 test_me.java 添加到您的清单中,之后它应该可以工作。

于 2014-10-10T09:18:53.540 回答