我已经按照本教程this tutorial设置了我的DatBas和主要活动以适应教程。 我收到了一个错误:
E / AndroidRuntime(281):java.lang.RuntimeException:无法启动活动ComponentInfo {com.xxx.xxx/com.xxx.xxx.Tamar_appActivity}:java.lang.NullPointerException
Tamar_appActivity代码为:
DatBas db = new DatBas(this);
TamarDatabaseCursor c = db.getActress();
if (c.moveToFirst())
DisplayRadioButton(c);
else
Toast.makeText(this, "No title found", Toast.LENGTH_LONG).show();
db.close();
}
public void DisplayRadioButton(Cursor c) {
for (int i = 1; i < (c.getColumnCount()); i++) {
RadioGroup radiogroup = (RadioGroup) findViewById(R.id.NameSelectGroup);
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(i);
rdbtn.setText(c.getString(i));
radiogroup.addView(rdbtn);
}
}
数据库代码是:
public class DatBas {
public static final String KEY_ROW_ID = "_id";
public static final String KEY_IMAGE_PATH = "uri";
public static final String KEY_NAME = "name";
public static final String KEY_GENDER = "gender";
public static final String KEY_BORN_DATE_YEAR = "age_year";
public static final String KEY_BORN_DATE_MONTH = "age_month";
public static final String KEY_BORN_DATE_DAY = "age_day";
private static final String DATABASE_NAME = "TamatDB";
private static final String DATABASE_TABLE_SETTINGS = "settings";
private static final int DATABASE_VERSION = 20;
private TamarDatabase thdb;
private static Context tcontext;
private SQLiteDatabase tdb;
private static class TamarDatabase extends SQLiteOpenHelper {
public TamarDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String ctSettings = "CREATE TABLE " + DATABASE_TABLE_SETTINGS
+ " ( " + KEY_ROW_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_IMAGE_PATH + " TEXT NOT NULL, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_GENDER + " TEXT NOT NULL, "
+ KEY_BORN_DATE_YEAR + " TEXT NOT NULL, "
+ KEY_BABY_DATE_MONTH + " TEXT NOT NULL, "
+ KEY_BABY_DATE_DAY + " TEXT NOT NULL);";
db.execSQL(ctSettings);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_SETTINGS);
onCreate(db);
}
}
public DatBas(Context c) {
tcontext = c;
}
public DatBas open() throws SQLiteException {
thdb = new TamarDatabase(tcontext);
tdb = thdb.getWritableDatabase();
return this;
}
public void close() {
thdb.close();
}
public long createEntrySettings(String bbdy, String bbdm, String bbdd,
String pt, String bg, String bfName) {
ContentValues cv2 = new ContentValues();
cv2.put(KEY_IMAGE_PATH, pt);
cv2.put(KEY_NAME, bfName);
cv2.put(KEY_GENDER, bg);
cv2.put(KEY_BORN_DATE_YEAR, bbdy);
cv2.put(KEY_BORN_DATE_MONTH, bbdm);
cv2.put(KEY_BORN_DATE_DAY, bbdd);
return tdb.insert(DATABASE_TABLE_SETTINGS, null, cv2);
}
public Cursor getDataSettings() {
String[] columns = new String[] { KEY_ROW_ID, KEY_IMAGE_PATH,
KEY_NAME, KEY_GENDER, KEY_BORN_DATE_YEAR,
KEY_BORN_DATE_MONTH, KEY_BORN_DATE_DAY };
Cursor c = tdb.query(DATABASE_TABLE_SETTINGS, columns, null, null,
null, null, null);
String results = "";
int iRawId = c.getColumnIndex(KEY_ROW_ID);
int iBIPath = c.getColumnIndex(KEY_IMAGE_PATH);
int iBName = c.getColumnIndex(KEY_NAME);
int iGender = c.getColumnIndex(KEY_GENDER);
int iBBDateYear = c.getColumnIndex(KEY_BORN_DATE_YEAR);
int iBBDateMonth = c.getColumnIndex(KEY_BORN_DATE_MONTH);
int iBBDateDay = c.getColumnIndex(KEY_BORN_DATE_DAY);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
results = results + " the kind is " + " " + c.getString(iRawId)
+ " the kind is " + " " + c.getString(iBIPath)
+ " the kind is " + " " + c.getString(iBName)
+ " the kind is " + " " + c.getString(iGender)
+ " the kind is " + " " + c.getString(iBBDateYear)
+ " the kind is " + " " + c.getString(iBBDateMonth)
+ " the kind is " + " " + c.getString(iBBDateDay) + "\n";
}
return c;
}
public String getDataSettingsBabyName() {
String[] columns = new String[] { KEY_ROW_ID, KEY_NAME};
Cursor c = tdb.query(DATABASE_TABLE_SETTINGS, columns, null, null,
null, null, null);
String results = "";
int iRawId = c.getColumnIndex(KEY_ROW_ID);
int iBName = c.getColumnIndex(KEY_NAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String[] resultsS = new String[]{c.getString(iRawId), c.getString(iBName)
};
}
return results;
}
public static class TamarDatabaseCursor extends SQLiteCursor{
/** The query for this cursor */
private static final String QUERY =
"SELECT _id, uri, baby_name, gender, age_year, age_month, age_day" +
" FROM settings";
/** Cursor constructor */
private TamarDatabaseCursor(SQLiteDatabase db, SQLiteCursorDriver driver,
String editTable, SQLiteQuery query) {
super(db, driver, editTable, query);
}
/** Private factory class necessary for rawQueryWithFactory() call */
private static class Factory implements SQLiteDatabase.CursorFactory{
public Cursor newCursor(SQLiteDatabase db,
SQLiteCursorDriver driver, String editTable,
SQLiteQuery query) {
return new TamarDatabaseCursor(db, driver, editTable, query);
}
}
/* Accessor functions get one per database column */
public int getActressId(){return getInt(getColumnIndexOrThrow("actress.actressId"));}
}
public TamarDatabaseCursor getActress()
{
SQLiteDatabase d = getReadableDatabase();
TamarDatabaseCursor c = (TamarDatabaseCursor)d.rawQueryWithFactory
(new TamarDatabaseCursor.Factory(), TamarDatabaseCursor.QUERY, null, null);
c.moveToFirst();
return c;
}
private SQLiteDatabase getReadableDatabase() {
// TODO Auto-generated method stub
return null;
}
public DatBas delete() {
tdb.delete(DATABASE_TABLE, null, null);
tdb.delete(DATABASE_TABLE_SETTINGS, null, null);
return null;
}
}
主XML是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3691c9"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/sideGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<RadioButton
android:id="@+id/rbtnLeft"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="50"
android:text="xxx" />
<RadioButton
android:id="@+id/rbtnRight"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="50"
android:text="xxx" />
</RadioGroup>
<RadioGroup
android:id="@+id/kindGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<RadioButton
android:id="@+id/rbtnN"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="50"
android:text="xxx" />
<RadioButton
android:id="@+id/rbtnP"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="50"
android:text="xxx" />
</RadioGroup>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView"
android:textColor="#363636"
android:textSize="25dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView"
android:textColor="#363636"
android:textSize="40dp"
android:textStyle="bold" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pause" />
<Button
android:id="@+id/Button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Resume" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop" />
<RadioGroup
android:id="@+id/NameSelectGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/bNameSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:text="RadioButton" />
</RadioGroup>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="38dp"
android:layout_height="37dp"
android:src="@android:drawable/ic_menu_manage" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_gravity="center"
android:src="@android:drawable/gallery_thumb" />
</LinearLayout>
答案 0 :(得分:0)
我会猜测findViewById(R.id.babyNameSelectGroup);
返回null,当你尝试radiogroup.addView(rdbtn);
时,你会得到NPE。
在尝试访问布局中包含的视图之前,您是否已完成setContentView(R.layout.yourmainlayout);
?
修改强>
您正在寻找babyNameSelectGroup
,这在您的布局中似乎不存在(从而为您提供NPE)。也许它应该是NameSelectGroup
?
答案 1 :(得分:0)
我必须设置getReadableDatabase()
方法:
public SQLiteDatabase getReadableDatabase() throws SQLiteException {
thdb = new TamarDatabase(tcontext);
tdb = thdb.getReadableDatabase();
return tdb;
}