无法实例化活动ComponentInfo {...}:java.lang.InstantiationException

时间:2013-03-05 13:15:52

标签: android apk

我已经尝试了我读过的所有线程的所有解决方案。 有什么建议吗?

我已经调试了应用程序,但异常是在内部代码od android中,并且没有行数。

这是班级

package com.beppe.reminder;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;

public class ReminderShow extends Activity{

private Context mCtx; 
private long ID;
private DBAdapter db;
private Cursor c;



ReminderShow(Context c){
    mCtx=c;
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    db=new DBAdapter(mCtx);
    setContentView(R.layout.reminder_show);


    TextView tit=(TextView)findViewById(R.id.title11);
    TextView bod=(TextView)findViewById(R.id.body11);
    TextView dat=(TextView)findViewById(R.id.date11);
    TextView tim=(TextView)findViewById(R.id.time11);
    if(getIntent()!=null){
        ID=getIntent().getExtras().getLong(DBAdapter.KEY_ROWID);
        db.open();
        c=db.fetchTaskById((int)ID);
        c.moveToFirst();
        tit.setText(c.getString(1));
        bod.setText(c.getString(2));
        dat.setText(c.getString(3).split(" ")[0]);
        tim.setText(c.getString(3).split(" ")[1]);
    }
}
}

这是清单

活动被正确声明,如果我从包中删除它,编译器会向我显示错误。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.beppe.reminder"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="10" />
<permission android:protectionLevel="normal" android:name="android.permission.WAKE_LOCK"></permission>
<permission android:protectionLevel="normal" android:name="android.permission.RECEIVE_BOOT_COMPLETED"></permission>
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.beppe.reminder.MainActivity"
        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="com.beppe.reminder.TaskEdit"
        android:label="@string/edit_label"></activity>
    <activity 
        android:name="com.beppe.reminder.ReminderShow"
        android:label="@string/show_label"></activity>
</application>

这里是logcat

enter code here03-05 15:36:33.474: W/dalvikvm(22257): threadid=1: thread exiting with uncaught exception (group=0x4001d560)
03-05 15:36:33.484: E/AndroidRuntime(22257): FATAL EXCEPTION: main
03-05 15:36:33.484: E/AndroidRuntime(22257): java.lang.RuntimeException: Unable to    instantiate activity ComponentInfo{com.beppe.reminder/com.beppe.reminder.ReminderShow}: java.lang.InstantiationException: com.beppe.reminder.ReminderShow
03-05 15:36:33.484: E/AndroidRuntime(22257):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at android.os.Looper.loop(Looper.java:130)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at android.app.ActivityThread.main(ActivityThread.java:3687)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at java.lang.reflect.Method.invokeNative(Native Method)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at java.lang.reflect.Method.invoke(Method.java:507)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at dalvik.system.NativeStart.main(Native Method)
03-05 15:36:33.484: E/AndroidRuntime(22257): Caused by:  java.lang.InstantiationException: com.beppe.reminder.ReminderShow
 03-05 15:36:33.484: E/AndroidRuntime(22257):   at java.lang.Class.newInstanceImpl(Native Method)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at java.lang.Class.newInstance(Class.java:1409)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-05 15:36:33.484: E/AndroidRuntime(22257):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
 03-05 15:36:33.484: E/AndroidRuntime(22257):   ... 11 more

2 个答案:

答案 0 :(得分:1)

因为您将null mCtx传递给DBAdapter类。将您的代码更改为:

public class ReminderShow extends Activity{

        private Context mCtx; 
        private long ID;
        private DBAdapter db;
        private Cursor c;


        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);

            setContentView(R.layout.reminder_show);
            db=new DBAdapter(ReminderShow.this);
             // your code here....

答案 1 :(得分:1)

删除您用于启动活动上下文的构造函数,因为您可以通过ReminderShow.this轻松获取上下文:

现在,您在行db=new DBAdapter(mCtx);

处传递空上下文

删除构造函数:

ReminderShow(Context c){
    mCtx=c;
}

并改变这个:

 db=new DBAdapter(mCtx);

db=new DBAdapter(ReminderShow.this);