无法弄清楚NullPointerException来自哪里

时间:2014-03-31 13:29:07

标签: java android sqlite

我正在尝试制作一个使用内置的SQLite数据库的Android应用程序。 我一直在努力使它工作,并尝试了几个修改。 当我通过eclipse在mye手机(android 4.2)或模拟器(Android 4.4)上启动应用程序时,我收到了几条消息。 我不知道NullPointerExceptions来自哪里

03-31 09:20:45.121: E/AndroidRuntime(991): FATAL EXCEPTION: main
03-31 09:20:45.121: E/AndroidRuntime(991): Process: com.example.julegaveliste2, PID: 991
03-31 09:20:45.121: E/AndroidRuntime(991): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.julegaveliste2/com.example.julegaveliste2.MainActivity}: java.lang.NullPointerException
03-31 09:20:45.121: E/AndroidRuntime(991):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
03-31 09:20:45.121: E/AndroidRuntime(991):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-31 09:20:45.121: E/AndroidRuntime(991):  at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-31 09:20:45.121: E/AndroidRuntime(991):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-31 09:20:45.121: E/AndroidRuntime(991):  at android.os.Handler.dispatchMessage(Handler.java:102)
03-31 09:20:45.121: E/AndroidRuntime(991):  at android.os.Looper.loop(Looper.java:137)
03-31 09:20:45.121: E/AndroidRuntime(991):  at android.app.ActivityThread.main(ActivityThread.java:4998)
03-31 09:20:45.121: E/AndroidRuntime(991):  at java.lang.reflect.Method.invokeNative(Native Method)
03-31 09:20:45.121: E/AndroidRuntime(991):  at java.lang.reflect.Method.invoke(Method.java:515)
03-31 09:20:45.121: E/AndroidRuntime(991):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-31 09:20:45.121: E/AndroidRuntime(991):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-31 09:20:45.121: E/AndroidRuntime(991):  at dalvik.system.NativeStart.main(Native Method)
03-31 09:20:45.121: E/AndroidRuntime(991): Caused by: java.lang.NullPointerException
03-31 09:20:45.121: E/AndroidRuntime(991):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
03-31 09:20:45.121: E/AndroidRuntime(991):  at com.example.julegaveliste2.MainActivity.<init>(MainActivity.java:16)
03-31 09:20:45.121: E/AndroidRuntime(991):  at java.lang.Class.newInstanceImpl(Native Method)
03-31 09:20:45.121: E/AndroidRuntime(991):  at java.lang.Class.newInstance(Class.java:1208)
03-31 09:20:45.121: E/AndroidRuntime(991):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-31 09:20:45.121: E/AndroidRuntime(991):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
03-31 09:20:45.121: E/AndroidRuntime(991):  ... 11 more

这是应用程序中的两个java文件:

    package com.example.julegaveliste2;



import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ListActivity {
    DatabaseHelper db = new DatabaseHelper(this.getApplicationContext());
    Button knapp1;
    Button knapp2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        knapp1 = (Button) findViewById(R.id.Legg_Til);
        knapp2 = (Button) findViewById(R.id.Les_database);

        knapp2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
            visliste();
            }
        });


       knapp1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                LagTabell(db.getWritableDatabase());
            }
        }); 

    }

    public static void LagTabell(SQLiteDatabase db)
    {
        ContentValues jul = new ContentValues();
        jul.put("person", "Ida");
        jul.put("gave", "blomst");
        db.insert("julegaveliste2", "person", jul); 
    }

    public void visliste()
    {   
        Cursor cursor=cursor();
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.activity_list_item, cursor, new String[]{"person","gave"}, new int[]{R.id.person,R.id.gave},0);    
        setListAdapter(adapter);
        cursor.close();

    }

    private Cursor cursor()
    {
        return(db.getWritableDatabase().rawQuery("SELECT _id, person, gave FROM julegaveliste", null));
    }


    @Override
    protected void onDestroy()
        {
            super.onDestroy();      
            db.close();
        }

}

package com.example.julegaveliste2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper
{
    private static final String DATABASE_NAME="julegaveliste2.db";
    private static final int SCHEMA=1;

    public DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, SCHEMA);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE julegaveliste2 (_id INTEGER PRIMARY KEY AUTOINCREMENT, person TEXT NOT NULL, gave TEXT);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        throw new RuntimeException("How did we get here?");
    }
}

3 个答案:

答案 0 :(得分:4)

 DatabaseHelper db = new DatabaseHelper(this.getApplicationContext());

您应该在db = new DatabaseHelper(this);

内移动onCreate()

答案 1 :(得分:3)

问题在于:

DatabaseHelper db = new DatabaseHelper(this.getApplicationContext());

之前无法获取Context,尤其是onCreate()

答案 2 :(得分:1)

将其替换为您的代码。

public class MainActivity extends ListActivity {
DatabaseHelper db;
Button knapp1;
Button knapp2;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

/*  Always initialize after setContentView      */
  db = new DatabaseHelper(MainActivity.this); //This will also work

    knapp1 = (Button) findViewById(R.id.Legg_Til);
    knapp2 = (Button) findViewById(R.id.Les_database);

    knapp2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
        visliste();
        }
    });
   knapp1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            LagTabell(db.getWritableDatabase());
        }
    }); 

}