SQLiteAssetHelper NullPointerException问题

时间:2018-10-31 20:29:15

标签: java android

我正在使用SqliteAssetHelper从资产文件夹复制我的数据库。 但是我正在收到NullPointerException。奇怪的是,它不是在所有设备上都在某些设备上发生的,我从未在模拟器或测试设备上得到它,但在游戏机崩溃报告中得到了它。以下是详细信息,请提前帮助我。请注意,下面提到的firsrname和secondname是我的应用程序ID的一部分

堆栈跟踪

java.lang.RuntimeException: 
at android.app.ActivityThread.performLaunchActivity    (ActivityThread.java:2412)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2607)
at android.app.ActivityThread.access$900 (ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1325)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:146)
at android.app.ActivityThread.main (ActivityThread.java:5756)
at java.lang.reflect.Method.invokeNative (Native Method)
at java.lang.reflect.Method.invoke (Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1107)
at dalvik.system.NativeStart.main (Native Method)
Caused by: java.lang.NullPointerException: 
at android.content.ContextWrapper.getApplicationInfo (ContextWrapper.java:165)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.<init> (SQLiteAssetHelper.java:109)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.<init> (SQLiteAssetHelper.java:129)
at com.firstname.secondname.firstnamesecondname.DatabaseHelperClass.<init> (DatabaseHelperClass.java:24)
at com.firstname.secondname.firstnamesecondname.DatabaseAccessClass.<init> (DatabaseAccessClass.java:24)
at com.firstname.secondname.firstnamesecondname.DatabaseAccessClass.getInstance (DatabaseAccessClass.java:35)
at com.firstname.secondname.firstnamesecondname.ActivityName.<init> (ActivityName.java:29
at java.lang.Class.newInstanceImpl (Native Method)
at java.lang.Class.newInstance (Class.java:1208)
at android.app.Instrumentation.newActivity (Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2403)

我的项目班级是

public class ActivityName extends AppCompatActivity {

CheckBox checkbtn;
String abc;
DatabaseAccessClass databaseAccess = DatabaseAccessClass.getInstance(this); //(Please note This is line number 29 in ActivityName)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_name);

    databaseAccess.open();

    Cursor cname = databaseAccess.getDetails(abc);
    cname.moveToFirst();
    String itemname = cname.getString(8);
    TextView iname = (TextView) findViewById(R.id.inametv);
    compname.setText(itemname);
    cname.close();

    //to add the current value to recent table
    addValueRecent();

    ViewPager viewPager = (ViewPager) findViewById(R.id.maintab);
    PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(),ActivityName.this);
    viewPager.setAdapter(pagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    databaseAccess.close();
}

public void addValueRecent(){

    databaseAccess.open();
    databaseAccess.insertValuerecent(abc);

    Cursor getallrecentb = databaseAccess.getAllRecentValues();

    if (getallrecentb.getCount()>20){

        getallrecentb.moveToLast();
        String id = getallrecentb.getString(0);
        databaseAccess.deleteValuerecent(id);
    }
    getallrecentb.close();
    databaseAccess.close();

}

DatabaseAccessClass 35行是

public static DatabaseAccessClass getInstance(Context context) {
    if (instance == null) {

        instance = new DatabaseAccessClass(context);//(this is line no 35 in DatabaseAccessClass)
    }
    return instance;
}

1 个答案:

答案 0 :(得分:0)

这行是您的问题:

g++

您必须按照以下步骤更新代码:

DatabaseAccessClass databaseAccess = DatabaseAccessClass.getInstance(this);

另外,这里:

DatabaseAccessClass databaseAccess;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_name);
    databaseAccess = DatabaseAccessClass.getInstance(this);
    ...
}

尝试获取应用程序的上下文。.

instance = new DatabaseAccessClass(context);