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



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

我尝试的是,在第一个活动中获取项目单击ID并将其传递给数据库类。但它不起作用。请帮助我解决问题。

从主活动类 .

            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 查询的方式。

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

日志猫

   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!

发生此错误是因为您的 getIntent 返回空引用,您应该检查在哪里调用 getIntent - 并且 getIntent() 需要一个在 onCreate() 之前不可用的上下文。因此,应该在活动生命周期中的onCreate方法内部或之后调用它。

希望这对您有所帮助。

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");

相关内容

  • 没有找到相关文章

最新更新