Passing value from extra to cursor

2019-09-08 07:39发布

问题:

I'm trying to iron out the last few bugs in my application. With my current setup I have the user go through a series of listview (Category -> source -> title). Based on the position from the listview I pass a int that is used by my cursor to filter the results and give the items that belong to the previously clicked item. After navigating a few times through the different activities it crashes since the application loses the value of said int (going back and forward through the activities). I'm trying to setup a getextra() so it will not lose this value. I've got it setup to put and get these ints but having trouble linking them to my cursor since it wants to access these int in a static way but I'm not getting them in a static way.

public class title extends ListActivity {

    Bundle extras = getIntent().getExtras();
    int categoryClick = extras.getInt("cateClick");
    int sourceClick = extras.getInt("sourceclick");

...

@Override
protected void onListItemClick(ListView list, View v, int position, long id)
{
    super.onListItemClick(list, v, position, id);
    titleClick = position;
    final Intent intent = new Intent(this, inputpage.class);
    intent.putExtra("cateclick", categoryClick);
    intent.putExtra("sourceclick", sourceClick);
    intent.putExtra("titleclick", titleClick);  
    startActivity(intent);
    }

My cursor unchanged from what I was using looks like this

// retrieves all the descriptions for the edittext fields
      public  Cursor getUserWord() 
        {
            return myDataBase.query(USER_WORD_TABLE, new String[] {
                    KEY_ID, 
                    KEY_CATEGORY,
                    KEY_SOURCE, KEY_TITLE, KEY_USERWORD, KEY_QUICK 
                    }, 
                    KEY_CATEGORY+ "=" + categories.categoryClick + " AND " + KEY_SOURCE+ "=" 
                    +source.sourceClick + " AND " + KEY_TITLE+ "=" + title.titleClick, 
                    null, null, null, KEY_ID);

        }

My database has multiple tables, the one for the above cursor is the image below.

This setup may not be the best method of getting the result I want but being new to android, sqlite and java it was what I was able to get to work for what I needed.

The error I was getting before trying to switch to getextra was

03-10 16:06:14.653: E/AndroidRuntime(939): Uncaught handler: thread main exiting due to uncaught exception
03-10 16:06:14.683: E/AndroidRuntime(939): java.lang.NullPointerException
03-10 16:06:14.683: E/AndroidRuntime(939):  at wanted.pro.madlibs.source.onListItemClick(source.java:55)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.app.ListActivity$2.onItemClick(ListActivity.java:312)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.widget.ListView.performItemClick(ListView.java:3285)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.os.Handler.handleCallback(Handler.java:587)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.os.Looper.loop(Looper.java:123)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.app.ActivityThread.main(ActivityThread.java:4363)
03-10 16:06:14.683: E/AndroidRuntime(939):  at java.lang.reflect.Method.invokeNative(Native Method)
03-10 16:06:14.683: E/AndroidRuntime(939):  at java.lang.reflect.Method.invoke(Method.java:521)
03-10 16:06:14.683: E/AndroidRuntime(939):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-10 16:06:14.683: E/AndroidRuntime(939):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-10 16:06:14.683: E/AndroidRuntime(939):  at dalvik.system.NativeStart.main(Native Method)

Here is a screen shot break down of what's going on. After changing int to static after changing extra to static

回答1:

Based on the screenshots, you shouldn't be making static references to your Bundles. Make them instance variables, and set them every time your onCreate or onResume method gets called.