Sahesh
Sahesh

Reputation: 885

On item click grid view and SQLite - Android

I want to populate a list view based on grid item click. If user clicks the first grid item I want to show the list view according to it. I have used a pre populate SQLite database here.

What I tried is , to get item click id in first activity and pass it to DB Class. But it's not working. Please help me to fix the issue.

From Main Activity class .

            grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {


                    Intent i=new Intent(MainActivity.this ,ContactView.class);
                    i.putExtra("ID_EXTRA",String.valueOf(id));
                    startActivity(i);

                }




            });

This is how I retrieved the clicked id and equal the variable to SQL Query.

public List<String> getQuotes() {
    List<String> list = new ArrayList<>();
    Integer value;

    Bundle extras = getIntent().getExtras();
    String a = extras.getString("ID_EXTRA");

    if(extras != null)
    {

        Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id='ID_EXTRA'", null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            list.add(cursor.getString(0));
            cursor.moveToNext();
            cursor.close();
        }
    }

  /*  if (passedVar != null) {


    }*/

    return list;
}

logcat

   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sra.hellosrilanka/com.example.sra.hellosrilanka.ContactView}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.example.sra.hellosrilanka.DBAccess.getQuotes(DBAccess.java:50)
        at com.example.sra.hellosrilanka.ContactView.onCreate(ContactView.java:29)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)

           

 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-01 20:09:52.960    1230-1312/system_process E/InputDispatcher﹕ channel '2d7e5e28 com.example.sra.hellosrilanka/com.example.sra.hellosrilanka.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

Upvotes: 0

Views: 363

Answers (2)

Vishal Chauhan
Vishal Chauhan

Reputation: 917

This error occurs because your getIntent returns null reference, you should check where you are calling getIntent - and getIntent() needs a Context which isn't available before onCreate(). So it should be called inside or after onCreate method in Activity life cycle.

Hope this will help you.

Upvotes: 1

Sush
Sush

Reputation: 3874

database.rawQuery("SELECT org_name FROM org_name WHERE category_id='ID_EXTRA'", null);

change this to

database.rawQuery("SELECT org_name FROM org_name WHERE category_id="+a, null);

i think instead of id, position has to be sent.. id is long (just a suggestion refer onclick handler)

Bundle extras = getIntent().getExtras();
    String a = extras.getString("ID_EXTRA");
change to 
 String a =getIntent().getStringExtra("ID_EXTRA");

Upvotes: 0

Related Questions