iem跟随youtube上有关在android中使用SQLite的教程,代码使应用程序在开始代码时崩溃
MainActivity.java
package com.example.asd;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
VivzHelper vivzHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vivzHelper = new VivzHelper(this);
SQLiteDatabase sqLiteDatabase = vivzHelper.getWritableDatabase();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Message.java
package com.example.asd;
import android.content.Context;
import android.widget.Toast;
public class Message {
public static void message(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}
VivzHelper.java
package com.example.asd;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class VivzHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "vivzdatabase";
private static final String TABLE_NAME = "VIVZTABLE";
private static final int DATABASE_VERSION = 1;
private static final String UID = "_id";
private static final String NAME = "Name";
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + UID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR(255));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS" + TABLE_NAME;
private Context context;
public VivzHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Message.message(context, "constructor called");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
Message.message(context, "onCreate called");
} catch (SQLException e) {
Message.message(context, "" + e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Message.message(context, "onUpgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
Message.message(context, "" + e);
}
}
}
该应用崩溃
SQLiteDatabase sqLiteDatabase = vivzHelper.getWritableDatabase();
错误日志
12-11 19:49:16.459: E/AndroidRuntime(6043): FATAL EXCEPTION: main
12-11 19:49:16.459: E/AndroidRuntime(6043): Process: com.example.asd, PID: 6043
12-11 19:49:16.459: E/AndroidRuntime(6043): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asd/com.example.asd.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.app.ActivityThread.access$900(ActivityThread.java:177)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.os.Handler.dispatchMessage(Handler.java:102)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.os.Looper.loop(Looper.java:135)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.app.ActivityThread.main(ActivityThread.java:5910)
12-11 19:49:16.459: E/AndroidRuntime(6043): at java.lang.reflect.Method.invoke(Native Method)
12-11 19:49:16.459: E/AndroidRuntime(6043): at java.lang.reflect.Method.invoke(Method.java:372)
12-11 19:49:16.459: E/AndroidRuntime(6043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
12-11 19:49:16.459: E/AndroidRuntime(6043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
12-11 19:49:16.459: E/AndroidRuntime(6043): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.widget.Toast.<init>(Toast.java:130)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.widget.Toast.makeText(Toast.java:427)
12-11 19:49:16.459: E/AndroidRuntime(6043): at com.example.asd.Message.message(Message.java:9)
12-11 19:49:16.459: E/AndroidRuntime(6043): at com.example.asd.VivzHelper.onCreate(VivzHelper.java:29)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
12-11 19:49:16.459: E/AndroidRuntime(6043): at com.example.asd.MainActivity.onCreate(MainActivity.java:18)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.app.Activity.performCreate(Activity.java:6178)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
12-11 19:49:16.459: E/AndroidRuntime(6043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
12-11 19:49:16.459: E/AndroidRuntime(6043): ... 10 more
我不知道看起来与视频完全相同的代码有什么问题???
最佳答案
问题是什么?
getWritableDatabase呼叫您的onCreate()
第一次调用此方法时,将打开数据库并调用onCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase,int,int)和/或onOpen(SQLiteDatabase)。
在onCreate()
中
Message.message(context, "onCreate called");
此时的
context
是null
,因为它在顶部被声明为成员变量,如下所示:private Context context;
但是它永远不会被初始化。您将
Context
从您的Activity
传递给db helper类构造函数。初始化成员变量context
时,将其传递给message()
函数时会为该变量提供一个值,从而摆脱了NPE
。我们是怎么来到这里的?
您的堆栈跟踪显示
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources android.content.Context.getResources()'
on a null object reference
因此,您正在尝试使用
Context
对象来“做某事”,在这种情况下,该操作是创建并显示Toast
对象。因为如果我们在堆栈跟踪中稍稍向上看到引用您的项目的第一行,就会发现at com.example.asd.Message.message(Message.java:9)
这是这条线
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
给定的堆栈跟踪在这里可能导致什么问题?是的,
context
是null
。这带我们回到
该应用程序在
SQLiteDatabase sqLiteDatabase = vivzHelper.getWritableDatabase();
上崩溃如您在帖子中所述。它在那里崩溃,不是因为该行中的某些内容明显错误,而是因为该行中的某些内容调用了其他需要帮助的内容。