我从android开发开始,当我实例化SQLiteOpenHelper的子类时遇到问题,因为当我这样做时,我的应用程序简单并没有启动。
这是我的活动。
package com.almostGames.sera;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class SeraActivity extends Activity {
Button btJogar, btPlacares, btDesafios, btHP;
ImagensDbHelper mDbHelper = new ImagensDbHelper(getApplicationContext());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
chamarTela1(); //metodo que chama a tela1
}
public void chamarTela1() {
setContentView(R.layout.home); //chama a tela
btJogar = (Button) findViewById(R.id.btnJogar);
btJogar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chamarTela2(); // Chama a tela2 ao clicar no botao
}
});
}
public void chamarTela2() {
setContentView(R.layout.seleciona_livros); //chama a tela
ViewPager galeria = (ViewPager) findViewById(R.id.galeria);
GaleriaImagensAdapter adapter = new GaleriaImagensAdapter(this);
galeria.setAdapter(adapter);
}
}
这是我的合同类
package com.almostGames.sera;
import android.database.sqlite.*;
import android.provider.BaseColumns;
public final class ImagensContrato {
// Construtor vazio
public ImagensContrato() {}
/* Definição do conteúdo da tabela*/
public static abstract class ImagensEntry implements BaseColumns {
public static final String TABLE_NAME = "imagens";
}
private String SQL_CREATE_ENTRIES =
"CREATE TABLE sera " +
" (_id INTEGER(11) NOT NULL," +
" livro VARCHAR(25) NOT NULL," +
" resposta VARCHAR(30) NOT NULL," +
" acertou INTEGER(11) NOT NULL," +
" localizacao BLOB," +
" PRIMARY KEY (_id)," +
" UNIQUE (_id))";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + ImagensEntry.TABLE_NAME;
public String getSqlCreateEntries() {
return SQL_CREATE_ENTRIES;
}
public String getSqlDeleteEntries() {
return SQL_DELETE_ENTRIES;
}
}
这是我的数据库助手
package com.almostGames.sera;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ImagensDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "sera.db";
ImagensContrato contrato = new ImagensContrato();
public ImagensDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(contrato.getSqlCreateEntries());
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(contrato.getSqlDeleteEntries());
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
这是例外
08-27 22:26:06.784: E/AndroidRuntime(25370): FATAL EXCEPTION: main
08-27 22:26:06.784: E/AndroidRuntime(25370): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.almostGames.sera/com.almostGames.sera.SeraActivity}: java.lang.NullPointerException
08-27 22:26:06.784: E/AndroidRuntime(25370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
08-27 22:26:06.784: E/AndroidRuntime(25370): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2178)
08-27 22:26:06.784: E/AndroidRuntime(25370): at android.app.ActivityThread.access$700(ActivityThread.java:141)
08-27 22:26:06.784: E/AndroidRuntime(25370): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
08-27 22:26:06.784: E/AndroidRuntime(25370): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 22:26:06.784: E/AndroidRuntime(25370): at android.os.Looper.loop(Looper.java:137)
08-27 22:26:06.784: E/AndroidRuntime(25370): at android.app.ActivityThread.main(ActivityThread.java:5118)
08-27 22:26:06.784: E/AndroidRuntime(25370): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 22:26:06.784: E/AndroidRuntime(25370): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 22:26:06.784: E/AndroidRuntime(25370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
08-27 22:26:06.784: E/AndroidRuntime(25370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
08-27 22:26:06.784: E/AndroidRuntime(25370): at dalvik.system.NativeStart.main(Native Method)
08-27 22:26:06.784: E/AndroidRuntime(25370): Caused by: java.lang.NullPointerException
08-27 22:26:06.784: E/AndroidRuntime(25370): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
08-27 22:26:06.784: E/AndroidRuntime(25370): at com.almostGames.sera.SeraActivity.<init>(SeraActivity.java:15)
08-27 22:26:06.784: E/AndroidRuntime(25370): at java.lang.Class.newInstanceImpl(Native Method)
08-27 22:26:06.784: E/AndroidRuntime(25370): at java.lang.Class.newInstance(Class.java:1319)
08-27 22:26:06.784: E/AndroidRuntime(25370): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-27 22:26:06.784: E/AndroidRuntime(25370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2055)
08-27 22:26:06.784: E/AndroidRuntime(25370): ... 11 more
我正在做这个教程(http://developer.android.com/training/basics/data-storage/databases.html),但是当我实例化我的DbHelper时,应用程序无法启动。
PS:我收到消息:&#34;遗憾的是已停止&#34;。
答案 0 :(得分:1)
ImagensDbHelper mDbHelper = new ImagensDbHelper(getApplicationContext());
此行崩溃且getApplicationContext()返回null,因为它在onCreate()方法之前被调用,而活动尚未创建。因此,尝试将此初始化移动到onCreate()方法中,如下所示:
ImagensDbHelper mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mDbHelper = new ImagensDbHelper(getApplicationContext());
chamarTela1(); //metodo que chama a tela1
}