【发布时间】: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