【问题标题】:getWritableDatabase throws a NullPointerExceptiongetWritableDatabase 抛出 NullPointerException
【发布时间】:2017-08-11 00:31:11
【问题描述】:

我遇到了一个问题,点击 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

如有任何帮助,我们将不胜感激。

【问题讨论】:

  • 您的代码中存在错误。你需要修复它。
  • 如果您需要帮助,请提供您的代码。另外,请考虑阅读有关如何提出好问题的文档。
  • 您的代码显示缺乏对意图/活动在 Android 中的工作方式的理解。从这里开始developer.android.com/training/index.html
  • 我认为您应该考虑为您的 DbAccess 扩展 AsyncTask

标签: java android sqlite nullpointerexception


【解决方案1】:

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

DbAccess db = new DbAccess();

public class DbAccess extends AppCompatActivity

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

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

【讨论】:

  • 第二个问题是new DbHelper(this)。 (提示:你的活动名称应该以Activity 结尾,即使你不能遵循这个烂摊子。)活动没有在构造函数中完全设置。每个将活动(或上下文)作为参数的实例化都应该在 onCreate 之前发生。
  • @EugenPechanec 通常是的,但在SQLiteOpenHelper 的情况下,Context 仅在get{Read,Writ}ableDatabase() 时使用(不在构造函数中)
  • @IaaIto 感谢您的回答。我还没有正确理解如何使用Context,我对编程还很陌生。在这种情况下,我如何传递有效的Context?如果我无法从MaterialsIn 实例化DbAccess,那么我该如何调用insertConcreteIn() 方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 2013-06-18
  • 2016-07-07
  • 2014-10-02
相关资源
最近更新 更多