2013-03-05 106 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
相關問題