我正在尝试从db中获取一个简单的图像和文本并在列表视图中显示它们,但是当调试器到达那个点时它会崩溃,我想知道为什么会发生这种情况?
突击表:
import android.provider.BaseColumns;
public class Schema {
public Schema() {
}
public static abstract class Raiding implements BaseColumns {
public static final String TABLE_NAME = "raiding";
public static final String _ID = "_ID";
public static final String ATTACK_NAME = "attack_name";
public static final String ATTACK_TH = "attack_th";
public static final String ATTACK_Type = "attack_type";
public static final String ATTACK_ElixirUsage = "elixir_usage";
public static final String ATTACK_Image = "attack_image";
}
}
dbHelper:包括两种方法: 1:向db添加新行 2:从db读取行:问题在这里,我猜,我使用调试器,当它要调用这个方法时,它突然崩溃
import java.util.ArrayList;
import java.util.List;
import sqlitemodel.Schema.Raiding;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MyDataBaseHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "strategies.db";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
public String CREATE_QUERY = "CREATE TABLE " + Raiding.TABLE_NAME + "("
+ Raiding._ID + " INTEGER PRIMARY KEY," + Raiding.ATTACK_NAME
+ TEXT_TYPE + COMMA_SEP + Raiding.ATTACK_TH + TEXT_TYPE + COMMA_SEP
+ Raiding.ATTACK_Type + TEXT_TYPE + COMMA_SEP
+ Raiding.ATTACK_ElixirUsage + TEXT_TYPE + COMMA_SEP
+ Raiding.ATTACK_Image + " Blob" + ")";
public MyDataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("db", "created");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.d("table!", "Created!");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
arg0.execSQL("DROP TABLE IF EXISTS " + Raiding.TABLE_NAME);
onCreate(arg0);
}
public void addRaiding(RaidingClass raidingClass) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Raiding.ATTACK_NAME, raidingClass.name);
values.put(Raiding.ATTACK_TH, raidingClass.th);
values.put(Raiding.ATTACK_Type, raidingClass.type);
values.put(Raiding.ATTACK_ElixirUsage, raidingClass.usage);
values.put(Raiding.ATTACK_Image, raidingClass.image);
db.insert(Raiding.TABLE_NAME, null, values);
db.close();
}
public List<RaidingClass> getAllRaidingAttacks() {
List<RaidingClass> raidingAttackList = new ArrayList<RaidingClass>();
String selectQuery = "SELECT * FROM " + Raiding.TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
Log.d("count", String.valueOf(cursor.getCount()));
if (cursor.moveToFirst()) {
do {
RaidingClass raidingClass = new RaidingClass();
raidingClass.setID(cursor.getInt(0));
raidingClass.setName(cursor.getString(1));
raidingClass.setTh(cursor.getString(2));
raidingClass.setType(cursor.getString(3));
raidingClass.setUsage(cursor.getString(4));
raidingClass.setImage(cursor.getBlob(5));
raidingAttackList.add(raidingClass);
} while (cursor.moveToNext());
}
db.close();
return raidingAttackList;
}
}
RaidingClass
public class RaidingClass {
public int id;
public String name;
public String th;
public String type;
public String usage;
public byte[] image;
public RaidingClass() {
}
public RaidingClass(int id, String name, String th, String type, String usage,
byte[] image) {
this.id = id;
this.name = name;
this.th = th;
this.type = type;
this.usage = usage;
this.image = image;
}
public RaidingClass(String name, String th, String type, String usage,
byte[] image) {
this.name = name;
this.th = th;
this.type = type;
this.usage = usage;
this.image = image;
}
public void setID(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setTh(String th) {
this.th = th;
}
public void setType(String type) {
this.type = type;
}
public void setUsage(String usage) {
this.usage = usage;
}
public void setImage(byte[] image) {
this.image = image;
}
}
mainActivity.java:
public class MainActivity extends Activity {
Button btnattack;
MyDataBaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDataBaseHelper(this);
dbHelper.getWritableDatabase();
Bitmap image = BitmapFactory.decodeResource(getResources(),
R.drawable.barcher);
//
// // convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte imageInByte[] = stream.toByteArray();
dbHelper.addRaiding(new RaidingClass("barcher", "8", "R", "many",
imageInByte));
btnattack = (Button) findViewById(R.id.btnattack);
btnattack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this,
RaidingAttack.class);
startActivity(myIntent);
}
});
}
}
RaidingAttack:这是我尝试调用getAllRaidingAttacks()方法来获取相应行值的类。
public class RaidingAttack extends Activity {
ArrayList<RaidingClass> raidingDetails = new ArrayList<RaidingClass>();
RaidingAdapter raidingAdapter;
MyDataBaseHelper dbHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_raiding);
dbHelper.getReadableDatabase();
List<RaidingClass> raiding = dbHelper.getAllRaidingAttacks();
for (RaidingClass rd : raiding) {
raidingDetails.add(rd);
}
raidingAdapter = new RaidingAdapter(this,
R.layout.activity_list_singlerow, raidingDetails);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(raidingAdapter);
}
}
这正是调试器停止和应用程序崩溃的地方:
List<RaidingClass> raiding = dbHelper.getAllRaidingAttacks();
我检查了数据库,插入了图像和文本,但我不知道为什么光标为空!
这是错误日志:
10-20 05:11:29.102: E/AndroidRuntime(14511): FATAL EXCEPTION: main
10-20 05:11:29.102: E/AndroidRuntime(14511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.masoudseraj.clashofclansstrategies/com.masoudseraj.clashofclansstrategies.RaidingAttack}: java.lang.NullPointerException
10-20 05:11:29.102: E/AndroidRuntime(14511): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-20 05:11:29.102: E/AndroidRuntime(14511): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-20 05:11:29.102: E/AndroidRuntime(14511): at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-20 05:11:29.102: E/AndroidRuntime(14511): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-20 05:11:29.102: E/AndroidRuntime(14511): at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 05:11:29.102: E/AndroidRuntime(14511): at android.os.Looper.loop(Looper.java:137)
10-20 05:11:29.102: E/AndroidRuntime(14511): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-20 05:11:29.102: E/AndroidRuntime(14511): at java.lang.reflect.Method.invokeNative(Native Method)
10-20 05:11:29.102: E/AndroidRuntime(14511): at java.lang.reflect.Method.invoke(Method.java:511)
10-20 05:11:29.102: E/AndroidRuntime(14511): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-20 05:11:29.102: E/AndroidRuntime(14511): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-20 05:11:29.102: E/AndroidRuntime(14511): at dalvik.system.NativeStart.main(Native Method)
10-20 05:11:29.102: E/AndroidRuntime(14511): Caused by: java.lang.NullPointerException
10-20 05:11:29.102: E/AndroidRuntime(14511): at com.masoudseraj.clashofclansstrategies.RaidingAttack.onCreate(RaidingAttack.java:24)
10-20 05:11:29.102: E/AndroidRuntime(14511): at android.app.Activity.performCreate(Activity.java:4465)
10-20 05:11:29.102: E/AndroidRuntime(14511): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-20 05:11:29.102: E/AndroidRuntime(14511): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-20 05:11:29.102: E/AndroidRuntime(14511): ... 11 more
答案 0 :(得分:1)
dbHelper未在RaidingAttack
public class RaidingAttack extends Activity {
ArrayList<RaidingClass> raidingDetails = new ArrayList<RaidingClass>();
RaidingAdapter raidingAdapter;
MyDataBaseHelper dbHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_raiding);
//init dbHelper
dbHelper = new MyDataBaseHelper(this);
dbHelper.getReadableDatabase();