android - 无法实例化 Activity ComponentInfo{...} : java. lang.InstantiationException

标签 android apk

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

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

这就是类(class)

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]);
    }
}
}

这是 list

该 Activity 声明正确,如果我从包中删除它,编译器会显示错误。

<?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>

这是日志猫

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

最佳答案

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

现在,您将在 db=new DBAdapter(mCtx); 行传递空上下文;

删除构造函数:

ReminderShow(Context c){
    mCtx=c;
}

并更改此:

 db=new DBAdapter(mCtx);

db=new DBAdapter(ReminderShow.this);

关于android - 无法实例化 Activity ComponentInfo{...} : java. lang.InstantiationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15224768/

相关文章:

android - 使用Unity编译后是否可以读取apk文件脚本?

android - 获取错误 : *****. 未找到 apk

reverse-engineering - Android apk 文件中的包依赖

android - 在微调器中显示 ActionBar 搜索结果

android - Activity fragment

android - 哪个gradle任务会构建AndroidTest。* apk,并且不会执行任何其他操作

android - 为什么我得到一个空对象引用 documentSnapshot.toObject() 方法?

AndroidX Fragment 添加谷歌地图

android - 无法在Google Play商店上发布Xamarin.Forms App的APK文件

java - 如何在 Eclipse for android 中设置自定义 keystore 以进行调试