getWritableDatabase抛出NullPointerException

时间:2017-03-19 20:02:56

标签: java android sqlite nullpointerexception

我遇到了一个问题,即点击concreteIn按钮会导致应用崩溃。

以下代码来自MaterialsIn类。

package com.example.android.gatekeeper;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.example.android.gatekeeper.gateData.DbAccess;

import static com.example.android.gatekeeper.MaterialsOut.REQUEST_IMAGE_CAPTURE;


public class MaterialsIn extends AppCompatActivity {

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

        // Find the (@Concrete) button.
        Button concreteIn = (Button) findViewById(R.id.concrete_in);

        // Set a click listener on that View
        concreteIn.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the Concrete In button is clicked on.
            @Override
            public void onClick(View view) {
                DbAccess db = new DbAccess();
                db.insertConcreteIn();
                Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePicture.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePicture, REQUEST_IMAGE_CAPTURE);
                }
            }
        });
    }
}

调用insertConcreteIn()方法时,应用程序在getWritableDatabase()崩溃。

package com.example.android.gatekeeper.gateData;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;

import com.example.android.gatekeeper.gateData.Contract.GateEntry;


public class DbAccess extends AppCompatActivity {

    // To access our database, we instantiate our subclass of SQLiteOpenHelper
    // and pass the context, which is the current activity.
    DbHelper mDbHelper = new DbHelper(this);

    // Insert data corresponding to the concrete in to the database.
    public void insertConcreteIn() {

        // Gets the data repository in write mode.
        SQLiteDatabase SQLiteDb = mDbHelper.getWritableDatabase();

        // Create a new map of values, where column names are the keys.
        ContentValues values = new ContentValues();
        values.put(GateEntry.COLUMN_DIRECTION, "To Site");
        values.put(GateEntry.COLUMN_TYPE, "Concrete");
        values.put(GateEntry.COLUMN_DATE, 170303); // Placeholder int for now.
        values.put(GateEntry.COLUMN_TIME, 171930); // Placeholder int for now.

        // Insert the new row, returning the primary key value of the new row.
        long newRowId = SQLiteDb.insert(GateEntry.TABLE_NAME, null, values);
    }
}

logcat如下;

03-19 19:58:08.190 24751-24751/com.example.android.gatekeeper E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.android.gatekeeper, PID: 24751
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                                    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:283)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                    at com.example.android.gatekeeper.gateData.DbAccess.insertConcreteIn(DbAccess.java:20)
                                                                                    at com.example.android.gatekeeper.MaterialsIn$1.onClick(MaterialsIn.java:34)
                                                                                    at android.view.View.performClick(View.java:5702)
                                                                                    at android.widget.TextView.performClick(TextView.java:10888)
                                                                                    at android.view.View$PerformClick.run(View.java:22541)
                                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:158)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
03-19 19:58:08.190 3464-5216/? W/ActivityManager:   Force finishing activity com.example.android.gatekeeper/.MaterialsIn

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题是您将无效的Context传递给SQLiteOpenHelper构造函数。那是因为

DbAccess db = new DbAccess();

public class DbAccess extends AppCompatActivity

永远不要自己实例化Activity课程。此类实例不适合您想要活动的任何内容,例如用作Context

相反,将框架设置的有效Context传递给需要它的方法。