简体   繁体   中英

CursorAdapter crashing app while trying to display listview (with content from database) - logcat inside

In my application, when clicked, a button "Favorites" is supposed to open a new window with a listview whose content is from a database. I got a NullPointerException when I click this button.

Here is the relevant code :

@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);
        }

    }); 
}  

This part of my logcat shows where the problem comes from :
在此处输入图片说明

When I commented out this line, there was no more NPE (but of course nothing was working):

listView.setAdapter(cursorAdapter);

What confuses me here is that in this very application, I have another button opening another window with a listview whose content is from another table of the database, and it works well. The code is very similar, I just changed names of the variables.
So, why can I open my listview and click elements just well in one case, and in this case, I got a NPE with a similar code ?! I have no idea where it can come from, maybe the table from my database? Thanks for your help, I am really struggling here.
EDIT : Here is the full 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  

My 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);
}

try this way.

  //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();
    }

You can first find if cursor value is not null then your displayCursor() is call.

If you are not getting solution then put comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM