0

我有一个用来扩展应用程序的类。这是它的代码:

package com.example.myapp

public class MainApplication extends Application {
    public ArrayList<Level> remoteLevels = new ArrayList<Level>();
    public ArrayList<Level> localLevels = new ArrayList<Level>();
    public TCMSQLiteHelper sqliteHelper = new TCMSQLiteHelper(this.getApplicationContext());

    @Override
    public void onCreate() {
        super.onCreate();
        this.loadRemoteLevels();
        localLevels = sqliteHelper.getAllLevels();
        if(localLevels.size()>0){
            Log.d("DEBUG","Explore Data Found");
        } else {
            Log.d("DEBUG","No Explore Data Found");
        }
    }
}

清单具有以下条目

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/TCMTheme"
    android:name="MainApplication">

我收到错误java.lang.RuntimeException: Unable to instantiate application com.example.myapp.MainApplication: java.lang.NullPointerException

我错过了什么?

4

1 回答 1

1

从您的代码中,您TCMSQLiteHelper在启动类之前定义

public TCMSQLiteHelper sqliteHelper = new TCMSQLiteHelper(this.getApplicationContext());
                                                           ^^^

试试这个方法:

public TCMSQLiteHelper sqliteHelper;

...

 @Override
public void onCreate() {

...
sqliteHelper = new TCMSQLiteHelper(this.getApplicationContext());
于 2013-10-17T18:51:28.773 回答