私のアプリケーションでは、クリックすると、「お気に入り」ボタンは、データベースからのコンテンツを含むリストビューを含む新しいウィンドウを開くことになっています。このボタンをクリックすると NullPointerException が発生しました。
関連するコードは次のとおりです。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorites_list_view);
dbHelper = new DatabaseAdapter(this);
dbHelper.open();
displayListView();
}
private void displayListView() {
c = dbHelper.findFavoritesInTable();
displayCursor();
}
private void displayCursor() {
String[] columns = new String[] { DatabaseAdapter.FAV_NAME,
DatabaseAdapter.FAV_CAT1, DatabaseAdapter.FAV_CAT2,
DatabaseAdapter.FAV_CAT3, DatabaseAdapter.FAV_CUISINE };
int[] to = new int[] { R.id.name, R.id.cat1, R.id.cat2, R.id.cat3,
R.id.cuisine };
cursorAdapter = new SimpleCursorAdapter(this, R.layout.poi_info, c,
columns, to, 0);
ListView listView = (ListView) findViewById(R.id.poilistview);
// Assign adapter to ListView
listView.setAdapter(cursorAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// When we click on elements :
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(getApplicationContext(),
POIActivity.class);
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String street = ((TextView) view.findViewById(R.id.street))
.getText().toString();
String website = ((TextView) view.findViewById(R.id.website))
.getText().toString();
String telephone = ((TextView) view
.findViewById(R.id.telephone)).getText().toString();
String remarks = ((TextView) view.findViewById(R.id.remarks))
.getText().toString();
String price = ((TextView) view.findViewById(R.id.price))
.getText().toString();
String mobile = ((TextView) view.findViewById(R.id.mobile))
.getText().toString();
String cat1 = ((TextView) view.findViewById(R.id.cat1))
.getText().toString();
String cat2 = ((TextView) view.findViewById(R.id.cat2))
.getText().toString();
String cat3 = ((TextView) view.findViewById(R.id.cat3))
.getText().toString();
String cuisine = ((TextView) view.findViewById(R.id.cuisine))
.getText().toString();
i.putExtra(ID_NAME, name);
i.putExtra(ID_STREET, street);
i.putExtra(ID_WEBSITE, website);
i.putExtra(ID_TELEPHONE, telephone);
i.putExtra(ID_REMARKS, remarks);
i.putExtra(ID_PRICE, price);
i.putExtra(ID_MOBILE, mobile);
i.putExtra(ID_CAT1, cat1);
i.putExtra(ID_CAT2, cat2);
i.putExtra(ID_CAT3, cat3);
i.putExtra(ID_CUISINE, cuisine);
startActivity(i);
}
});
}
私のlogcatのこの部分は、問題がどこから来たのかを示しています:
この行をコメントアウトすると、NPE はなくなりました (もちろん、何も機能していませんでした)。
listView.setAdapter(cursorAdapter);
ここで私を混乱させているのは、まさにこのアプリケーションで、別のウィンドウを開く別のボタンがあり、その内容がデータベースの別のテーブルからのものであり、それがうまく機能することです。コードは非常に似ています。変数の名前を変更しただけです。
では、リストビューを開いて要素をクリックできるのはなぜですか? それがどこから来るのかわかりません。おそらく私のデータベースのテーブルですか?助けてくれてありがとう、私はここで本当に苦労しています。
編集:ここに完全な logcat があります:
04-12 11:45:03.779: W/dalvikvm(2998): threadid=1: thread exiting with uncaught exception (group=0x40a95390)
04-12 11:45:03.779: E/AndroidRuntime(2998): FATAL EXCEPTION: main
04-12 11:45:03.779: E/AndroidRuntime(2998): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asiatypeapplicationbeta/com.example.asiatypeapplicationbeta.FavoritesListView}: java.lang.NullPointerException
04-12 11:45:03.779: E/AndroidRuntime(2998): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1964)
04-12 11:45:03.779: E/AndroidRuntime(2998): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1989)
04-12 11:45:03.779: E/AndroidRuntime(2998): at android.app.ActivityThread.access$600(ActivityThread.java:126)
04-12 11:45:03.779: E/AndroidRuntime(2998): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1155)
04-12 11:45:03.779: E/AndroidRuntime(2998): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 11:45:03.779: E/AndroidRuntime(2998): at android.os.Looper.loop(Looper.java:137)
04-12 11:45:03.779: E/AndroidRuntime(2998): at android.app.ActivityThread.main(ActivityThread.java:4482)
04-12 11:45:03.779: E/AndroidRuntime(2998): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 11:45:03.779: E/AndroidRuntime(2998): at java.lang.reflect.Method.invoke(Method.java:511)
04-12 11:45:03.779: E/AndroidRuntime(2998): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-12 11:45:03.779: E/AndroidRuntime(2998): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-12 11:45:03.779: E/AndroidRuntime(2998): at dalvik.system.NativeStart.main(Native Method)
04-12 11:45:03.779: E/AndroidRuntime(2998): Caused by: java.lang.NullPointerException
04-12 11:45:03.779: E/AndroidRuntime(2998): at com.example.asiatypeapplicationbeta.FavoritesListView.displayCursor(FavoritesListView.java:62)
04-12 11:45:03.779: E/AndroidRuntime(2998): at com.example.asiatypeapplicationbeta.FavoritesListView.displayListView(FavoritesListView.java:46)
04-12 11:45:03.779: E/AndroidRuntime(2998): at com.example.asiatypeapplicationbeta.FavoritesListView.onCreate(FavoritesListView.java:40)
04-12 11:45:03.779: E/AndroidRuntime(2998): at android.app.Activity.performCreate(Activity.java:4465)
04-12 11:45:03.779: E/AndroidRuntime(2998): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-12 11:45:03.779: E/AndroidRuntime(2998): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
04-12 11:45:03.779: E/AndroidRuntime(2998): ... 11 more
私の DatabaseAdapter.class :
public class DatabaseAdapter {
public static final String DATABASE_POITABLE = "tblpoisurvey";
public static final String DATABASE_FAVTABLE = "tblfavorites";
public static final String KEY_ROWID = "_id";
public static final String POI_NAME = "name";
public static final String POI_CAT1 = "cat1";
public static final String POI_CAT2 = "cat2";
public static final String POI_CAT3 = "cat3";
public static final String POI_SUBCAT = "subcat";
public static final String POI_STREET = "street";
public static final String POI_WEBSITE = "website";
public static final String POI_REMARKS = "remarks";
public static final String POI_TELEPHONE = "telephoneNo";
public static final String POI_MOBILE = "mobileNo";
public static final String POI_PRICE = "priceLevel";
public static final String POI_DATEFRIENDLY = "dateFriendly";
public static final String POI_PAYMENT = "paymentOptions";
public static final String POI_CUISINE = "cuisine";
// public static final String COL_X = "x";
// public static final String COL_Y = "y";
public static final String FAV_ID = "_id";
public static final String FAV_NAME = "name";
public static final String FAV_CAT1 = "cat1";
public static final String FAV_CAT2 = "cat2";
public static final String FAV_CAT3 = "cat3";
public static final String FAV_STREET = "street";
public static final String FAV_WEBSITE = "website";
public static final String FAV_REMARKS = "remarks";
public static final String FAV_TELEPHONE = "telephoneNo";
public static final String FAV_MOBILE = "mobileNo";
public static final String FAV_PRICE = "priceLevel";
public static final String FAV_DATEFRIENDLY = "dateFriendly";
public static final String FAV_PAYMENT = "paymentOptions";
public static final String FAV_CUISINE = "cuisine";
private Context myContext;
private static SQLiteDatabase myDatabase;
private DatabaseHelper dbHelper;
private Cursor c;
// Constructor
public DatabaseAdapter(Context context) {
this.myContext = context;
}
public DatabaseAdapter open() throws SQLException {
dbHelper = new DatabaseHelper(myContext);
try {
dbHelper.createDatabase();
} catch (IOException e) {
e.printStackTrace();
}
myDatabase = dbHelper.getWritableDatabase();
return this;
}
public void close() {
if (c != null) {
c.close();
}
try {
dbHelper.close();
myDatabase.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Method findPoiInTable, called when the user clicks on a level2 icon
// button to open the listview
public Cursor findPoiInTable(String categoryInterval) {
String where = POI_CAT1 + " IN " + categoryInterval + " OR " + POI_CAT2
+ " IN " + categoryInterval + " OR " + POI_CAT3 + " IN "
+ categoryInterval;
Cursor c = myDatabase.query(DATABASE_POITABLE, new String[] {
KEY_ROWID, POI_NAME, POI_STREET, POI_WEBSITE, POI_PAYMENT,
POI_TELEPHONE, POI_PRICE, POI_REMARKS, POI_DATEFRIENDLY,
POI_MOBILE, POI_CAT1, POI_CAT2, POI_CAT3, POI_CUISINE }, where,
null, null, null, POI_NAME);
return c;
}
public Cursor findFavoritesInTable() {
Cursor c = myDatabase.rawQuery("select * from tblfavorites",null);
return c;
}
public static long insertInTable(String ID_NAME, String ID_CAT1,
String ID_CAT2, String ID_CAT3, String ID_CUISINE) {
ContentValues data = new ContentValues();
data.put(FAV_NAME, ID_NAME);
data.put(FAV_CAT1, ID_CAT1);
data.put(FAV_CAT2, ID_CAT2);
data.put(FAV_CAT3, ID_CAT3);
data.put(FAV_CUISINE, ID_CUISINE);
if (myDatabase == null) {
}
return myDatabase.insert("DATABASE_FAVTABLE", "nullColumnHack", data);
}