我有问题。我是Android和Java的新手。我尝试使用Database Helper类来读取txt文件并插入数据库表中。
以下是错误列表:
09-17 18:46:56.950 26633-26640/? E/zygote64: Failed sending reply to debugger: Broken pipe
09-17 18:47:03.032 26633-26633/com.example.koziol.nukaang E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist!
09-17 18:47:03.035 26633-26633/com.example.koziol.nukaang E/Minikin: Could not get cmap table size!
09-17 18:47:03.379 26633-26633/com.example.koziol.nukaang E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.koziol.nukaang, PID: 26633
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.koziol.nukaang/com.example.koziol.nukaang.StartActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
**caused by: java.lang.nullpointerexception: attempt to invoke virtual method 'android.content.context android.content.context.getapplicationcontext()' on a null object reference**
**at com.example.koziol.nukaang.DbHelper.onCreate(DbHelper.java:56)**
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:310)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:206)
at com.example.koziol.nukaang.DbHelper.<init>(DbHelper.java:47)
at com.example.koziol.nukaang.StartActivity.onCreate(StartActivity.java:32)
at android.app.Activity.performCreate(Activity.java:7372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
我尝试在onCreate类中放置Insert Table,这里有几种不同的方法,这是dbhelper类
public class DbHelper extends SQLiteOpenHelper {
private static String DataBase_Name;
private static int DataBase_Version;
static {
DataBase_Name = "MyDataBase";
DataBase_Version = 4;
}
public SQLiteDatabase db;
public List<Question> getQuestion;
public String[] Flashcard;
private Context ctx;
public DbHelper(Context context) {
super(context, DataBase_Name, null, DataBase_Version);
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
this.db = db;
ctx.getApplicationContext();
String SQL_CREATE_DICTIONARY_TABLE = "CREATE TABLE " +
QuestionTable.TABLE_NAME + " ( " +
QuestionTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
QuestionTable.ANSWER + " TEXT, " +
QUESTION + " TEXT )";
db.execSQL(SQL_CREATE_DICTIONARY_TABLE);
try {
InsertTable();
} catch (IOException e) {
e.printStackTrace();
}
}
public void InsertTable(Context ctx) throws IOException {
try {
AssetManager am = ctx.getAssets();
InputStream is = am.open("dictDB.txt");
BufferedReader br = null;
br = new BufferedReader(new InputStreamReader(is));
String linee = null;
List<String> lines = new ArrayList<>();
while ((linee = br.readLine()) != null) {
lines.add(linee);
}
for (String line : lines)
{
db.execSQL("INSERT INTO " + QuestionTable.TABLE_NAME + "( " + QuestionTable.ANSWER + ", " + QuestionTable.QUESTION + " )" + "VALUES (" + line + ")");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void AddRow(String Eng, String Pl) {
db.execSQL("INSERT INTO "+QuestionTable.TABLE_NAME+"( "+ QuestionTable.ANSWER+", "+QuestionTable.QUESTION + ")" +"VALUES ("+Eng+", "+Pl,null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ QuestionTable.TABLE_NAME );
onCreate(db);
}
@RequiresApi(api = Build.VERSION_CODES.N)
public List<Question> getQuestion(){
List<Question> QuestionList = new ArrayList<>();
db = getReadableDatabase();
Cursor QuestionCursor = db.rawQuery("SELECT "+QuestionTable.ANSWER+", "+ QUESTION +" FROM "+ QuestionTable.TABLE_NAME + " ORDER BY RANDOM() LIMIT 10", null);
if (QuestionCursor.moveToFirst()){
do {
Question question = new Question();
question.setAnswer(QuestionCursor.getString((QuestionCursor.getColumnIndex(QuestionTable.ANSWER))));
question.setQuestion(QuestionCursor.getString(QuestionCursor.getColumnIndex(QUESTION)));
QuestionList.add(question);
} while (QuestionCursor.moveToNext());
}
QuestionCursor.close();
db.close();
return QuestionList;
}
public String[] Flashcard()
{
String id;
String EngWord;
String PolWord;
String Card[]=null;
db= getReadableDatabase();
id = db.rawQuery("SELECT " + QuestionTable._ID +"FROM " + QuestionTable.TABLE_NAME+ " ORDER BY RANDOM() LIMIT 1",null).toString();
EngWord = db.rawQuery("SELECT " + QuestionTable.ANSWER +"FROM " + QuestionTable.TABLE_NAME+ "WHERE "+(id==QuestionTable._ID.toString()),null).toString();
PolWord = db.rawQuery("SELECT " + QUESTION +"FROM " + QuestionTable.TABLE_NAME+ "WHERE "+(id==QuestionTable._ID.toString()),null).toString();
Card = new String[] {EngWord, PolWord};
db.close();
return Card;
}
}
public DbHelper(Context context) {
super(context, DataBase_Name, null, DataBase_Version);
db = getWritableDatabase();
ctx = context; //Add this line...
}
此外,当您已经拥有上下文引用ctx时,不确定是否需要调用getApplicationContext。
我的2美分......