0

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.enter image description here

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. enter image description here After changing int to static enter image description here after changing extra to static enter image description here

maebe
  • 553
  • 1
  • 6
  • 18
  • From the code you've shared, its not clear what you mean by: " 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." What are the static variables your cursor is accessing? – Eric Levine Mar 10 '12 at 22:06
  • If I were to change the cursor to // retrieves all the titles public Cursor getTitles() { return myDataBase.query(TITLE_TABLE, new String[] { KEY_ID, KEY_TITLE, KEY_TITLEDESC, KEY_TITLESTORY, KEY_CATEGORY, KEY_SOURCE, }, KEY_CATEGORY+ "=" + title.categoryClick + " AND " + KEY_SOURCE+ "=" +title.sourceClick, null, null, null, KEY_TITLEDESC); } – maebe Mar 10 '12 at 22:08
  • to get the int I pull from the previous intent it wants me to change int sourceClick = extras.getInt("sourceclick"); to static. Which if I do that Bundle extras = getIntent().getExtras(); can't be accessed in a static way. – maebe Mar 10 '12 at 22:09
  • What is the stack trace of the exception you get when your app crashes? – Eric Levine Mar 10 '12 at 22:10
  • Added logcat to question. With that log it crashed on the source class listview when I went to click it. But I get similar error on any of the listviews. – maebe Mar 10 '12 at 22:13
  • Also adding some screen shots of what I was trying to explain. Hope they make it more clear. – maebe Mar 10 '12 at 22:22
  • 1
    So which line is 55 (should be within onListItemClick)? – Eric Levine Mar 10 '12 at 22:37

1 Answers1

0

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.

Eric Levine
  • 13,536
  • 5
  • 49
  • 49
  • So which line is 55 (should be within onListItemClick)? That is the line that //titleClickSound.start(); I was barking up the wrong tree here. +1 for helping me seeing the error in my debugging. So not an issue with my position but an issue with my final MediaPlayer titleClickSound = MediaPlayer.create(this, R.raw.button50); titleClickSound.start(); Not sure why it works sometimes and other times crashes but gets me on the correct path. Commenting out the line prevents the error. – maebe Mar 10 '12 at 23:08
  • All fixed. TY for time. Accepting your answer to give rep for helping me through this. – maebe Mar 10 '12 at 23:30