在我的应用程序中,单击时,“收藏夹”按钮应该打开一个新窗口,其中包含一个列表视图,其内容来自数据库。单击此按钮时出现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);
让我感到困惑的是,在这个应用程序中,我有另一个按钮打开另一个带有listview的窗口,其内容来自数据库的另一个表,它运行良好。代码非常相似,我只是更改了变量的名称
那么,为什么我可以在一个案例中打开我的列表视图并单击元素,在这种情况下,我有一个类似代码的NPE?!我不知道它可以来自哪里,也许我的数据库中的表?谢谢你的帮助,我真的在这里挣扎。
编辑:以下是完整的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);
}
答案 0 :(得分:1)
尝试这种方式。
//check DatabaseAdapter is your database name with single parameter constructor
DatabaseAdapter DdbHelper = new DatabaseAdapter(this);
dbHelper.open();
Cursor c = dbHelper.findFavoritesInTable();
if(c.getCount()!=0){
System.out.println("CURSOR VALUE:"+c.getCount());
displayCursor();
}
c.close();
dbHelper.close();
}
您可以先找到游标值是否为空,然后调用displayCursor()
。
如果你没有得到解决方案,那就发表评论。