android - AlertDialog 导致 java.lang.RuntimeException : Unable to instantiate activity ComponentInfo

标签 android runtime android-alertdialog

在单击“警报对话框确定”按钮以 Intent 执行下一个 Activity 后,我收到此错误,数据仍在插入数据库中,但应用程序崩溃了。

Logcat 错误:

07-22 02:26:08.603: E/JSON(10170): {"tag":"register","success":1,"error":0,"uid":"500af44ea075b5.28543539","user":{"name":"Dddds","email":"Saasa","imei":"359616041330986","created_at":"2012-07-22 02:26:22","updated_at":null}}
07-22 02:26:08.623: E/JSON(10170): {"uid":"500af44ea075b5.28543539","error":0,"user":{"created_at":"2012-07-22 02:26:22","updated_at":null,"email":"Saasa","imei":"359616041330986","name":"Dddds"},"success":1,"tag":"register"}
07-22 02:26:08.758: D/dalvikvm(10170): GC_CONCURRENT freed 142K, 3% free 13469K/13831K, paused 7ms+2ms
07-22 02:26:09.608: D/dalvikvm(10170): newInstance failed: p0 i0 [0 a1
07-22 02:26:09.608: D/AndroidRuntime(10170): Shutting down VM
07-22 02:26:09.608: W/dalvikvm(10170): threadid=1: thread exiting with uncaught exception (group=0x40c681f8)
07-22 02:26:09.613: E/AndroidRuntime(10170): FATAL EXCEPTION: main
07-22 02:26:09.613: E/AndroidRuntime(10170): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{nyp.android.project/nyp.android.project.DashboardActivity}: java.lang.InstantiationException: can't instantiate class nyp.android.project.DashboardActivity
07-22 02:26:09.613: E/AndroidRuntime(10170):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at android.os.Looper.loop(Looper.java:137)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at android.app.ActivityThread.main(ActivityThread.java:4511)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at java.lang.reflect.Method.invokeNative(Native Method)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at java.lang.reflect.Method.invoke(Method.java:511)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at dalvik.system.NativeStart.main(Native Method)
07-22 02:26:09.613: E/AndroidRuntime(10170): Caused by: java.lang.InstantiationException: can't instantiate class nyp.android.project.DashboardActivity
07-22 02:26:09.613: E/AndroidRuntime(10170):    at java.lang.Class.newInstanceImpl(Native Method)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at java.lang.Class.newInstance(Class.java:1319)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
07-22 02:26:09.613: E/AndroidRuntime(10170):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
07-22 02:26:09.613: E/AndroidRuntime(10170):    ... 11 more
07-22 02:26:59.308: I/Process(10170): Sending signal. PID: 10170 SIG: 9
07-22 02:31:13.878: D/OpenGLRenderer(10938): Flushing caches (mode 0)
07-22 02:31:15.773: D/dalvikvm(10938): GC_CONCURRENT freed 155K, 3% free 13114K/13511K, paused 2ms+2ms
07-22 02:31:17.708: D/OpenGLRenderer(10938): Flushing caches (mode 0)
07-22 02:31:26.963: E/JSON(10938): {"tag":"register","success":1,"error":0,"uid":"500af58d0aa288.05836094","user":{"name":"Test","email":"Test","imei":"359616041330986","created_at":"2012-07-22 02:31:41","updated_at":null}}
07-22 02:31:26.968: E/JSON(10938): {"uid":"500af58d0aa288.05836094","error":0,"user":{"created_at":"2012-07-22 02:31:41","updated_at":null,"email":"Test","imei":"359616041330986","name":"Test"},"success":1,"tag":"register"}
07-22 02:31:27.008: D/dalvikvm(10938): GC_FOR_ALLOC freed 214K, 3% free 13240K/13639K, paused 20ms
07-22 02:31:28.468: D/dalvikvm(10938): newInstance failed: p0 i0 [0 a1
07-22 02:31:28.468: D/AndroidRuntime(10938): Shutting down VM
07-22 02:31:28.468: W/dalvikvm(10938): threadid=1: thread exiting with uncaught exception (group=0x40c681f8)
07-22 02:31:28.473: E/AndroidRuntime(10938): FATAL EXCEPTION: main
07-22 02:31:28.473: E/AndroidRuntime(10938): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{nyp.android.project/nyp.android.project.DashboardActivity}: java.lang.InstantiationException: can't instantiate class nyp.android.project.DashboardActivity
07-22 02:31:28.473: E/AndroidRuntime(10938):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at android.os.Looper.loop(Looper.java:137)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at android.app.ActivityThread.main(ActivityThread.java:4511)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at java.lang.reflect.Method.invokeNative(Native Method)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at java.lang.reflect.Method.invoke(Method.java:511)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at dalvik.system.NativeStart.main(Native Method)
07-22 02:31:28.473: E/AndroidRuntime(10938): Caused by: java.lang.InstantiationException: can't instantiate class nyp.android.project.DashboardActivity
07-22 02:31:28.473: E/AndroidRuntime(10938):    at java.lang.Class.newInstanceImpl(Native Method)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at java.lang.Class.newInstance(Class.java:1319)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
07-22 02:31:28.473: E/AndroidRuntime(10938):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
07-22 02:31:28.473: E/AndroidRuntime(10938):    ... 11 more

代码:

            JSONObject json = userFunction.registerUser(name, email, password, IMEI.getText().toString());

            // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    registerErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS); 
                    if(Integer.parseInt(res) == 1){
                        // user successfully registered
                        // Store user details in SQLite Database
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this).create();

                        // Setting Dialog Title
                        alertDialog.setTitle("Registration");

                        // Setting Dialog Message
                        alertDialog.setMessage("Registration successful");

                        // Setting Icon to Dialog
                        alertDialog.setIcon(R.drawable.tick);

                        // Setting OK Button
                        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                        // Write your code here to execute after dialog closed
                        //Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();

                        // Launch Dashboard Screen
                        Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

                        // Close all views before launching Dashboard
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(dashboard);

                        // Close Registration Screen
                        finish();

                        }
                        });

                        // Showing Alert Message
                        alertDialog.show();     

                        // Clear all previous data in database
                        userFunction.logoutUser(getApplicationContext());
                        db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT), json_user.getString(KEY_IMEI));                     

                    }else{
                        // Error in registration
                        registerErrorMsg.setText("Error occured in registration");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

最佳答案

我从未见过使用 getApplicationContext() 创建的 Intent 。您是否尝试使用 ClassName.this 来代替?

通常是:

Intent intent = new Intent(CurrentActivity.this, NextActivity.class);

关于android - AlertDialog 导致 java.lang.RuntimeException : Unable to instantiate activity ComponentInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11595023/

相关文章:

runtime - append 过程的运行时是否为O(n)?

android - 如何在 android 中以编程方式与 USSD 对话框交互

android - 如何制作自定义警报对话框全屏

jsf - 在运行时从支持 bean 添加 JSF 2 复合组件

android - 如何从 Android 中的网站 url 获取 Json 数组?

android - 如何将设置 Activity 合并到主要 Activity ?

android - ViewPager + Picasso + 来自 URL 字符串数组的多个图像

c - 使用多个文件 C 与将其全部放入单个文件相比,二进制文件有区别吗?

android - 我们可以在警报对话框中使用 Assets html 文件吗?

java - 解析 XML 时出错(一些烦人的问题,初学者)