2013-03-05 81 views
2

我试过了我读过的所有线程的所有解决方案。 有什么建议吗?无法实例化活动ComponentInfo {...}:java.lang.InstantiationException

我已经调试了应用程序,但例外情况是在内部代码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 
+0

是否有异常没有_caused by_? – 2013-03-05 13:36:18

回答

1

删除构造函数:

现在你是路过这里null上下文行db=new DBAdapter(mCtx);

删除构造:

​​

和更改此:

db=new DBAdapter(mCtx); 

db=new DBAdapter(ReminderShow.this); 
+0

这解决了这个问题。你能告诉我为什么构造函数导致这个问题? – user2135833 2013-03-05 14:55:43

1

因为你传递null mCtxDBAdapter类。改变你的代码:

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.... 
您已用于启动活动的情况下,你会很容易地得到您的上下文这里通过 ReminderShow.this
相关问题