在项目上单击网格视图和 SQLite - Android
On item click grid view and SQLite - Android
我想根据网格项目点击填充列表视图。如果用户单击第一个网格项,我想根据它显示列表视图。我在这里使用了预填充 SQLite 数据库。
我尝试的是首先 activity 获取项目点击 ID 并将其传递给数据库 Class。但它不起作用。请帮我解决这个问题。
来自主要 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);
}
});
这就是我检索点击的 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;
}
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.access0(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.access0(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!
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");
发生此错误是因为您的 getIntent returns 空引用,您应该检查调用 getIntent 的位置 - getIntent() 需要一个在 onCreate() 之前不可用的上下文。所以它应该在 Activity 生命周期的 onCreate 方法内部或之后调用。
希望对您有所帮助。
我想根据网格项目点击填充列表视图。如果用户单击第一个网格项,我想根据它显示列表视图。我在这里使用了预填充 SQLite 数据库。
我尝试的是首先 activity 获取项目点击 ID 并将其传递给数据库 Class。但它不起作用。请帮我解决这个问题。
来自主要 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);
}
});
这就是我检索点击的 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;
}
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.access0(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.access0(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!
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");
发生此错误是因为您的 getIntent returns 空引用,您应该检查调用 getIntent 的位置 - getIntent() 需要一个在 onCreate() 之前不可用的上下文。所以它应该在 Activity 生命周期的 onCreate 方法内部或之后调用。
希望对您有所帮助。