在项目点击网格视图和SQLite - Android

时间:2016-06-01 10:39:06

标签: android sqlite android-gridview onitemclick

我想根据网格项目点击填充列表视图。如果用户单击第一个网格项,我想根据它显示列表视图。我在这里使用了一个预先填充的SQLite数据库。

我尝试的是,在第一个活动中获取项目点击ID并将其传递给DB 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);

                }




            });

这是我检索单击的id并将变量等于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!

2 个答案:

答案 0 :(得分:1)

发生此错误是因为你的getIntent返回null引用,你应该检查你在哪里调用getIntent - 而getIntent()需要一个在onCreate()之前不可用的Context。所以它应该在Activity生命周期中的onCreate方法内部或之后调用。

希望这会对你有所帮助。

答案 1 :(得分:0)

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

将此更改为

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

我认为不是id,而是必须发送位置.. id很长(只是建议参考onclick处理程序)

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