在我的DataBaseAdapter类中,我编写了一个像这样的方法getAll
public List<AllUserInfor> getAllInfor(int id) {
List<AllUserInfor> allInfor = new ArrayList<AllUserInfor>();
String selectQuery = "SELECT Name, Gender FROM MY_TABLE where _id = '"+id+"' ";
Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
AllUserInfor alluserinfor = new AllUserInfor();
alluserinfor.setName(cursor.getString(1));
alluserinfor.setGender(cursor.getString(2));
allInfor.add(alluserinfor);
} while (cursor.moveToNext());
}
return allInfor;
}
在第二项活动中,我有
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btShow:
// I get id from the first activity
Bundle extras = getIntent().getExtras();
id = extras.getInt("Roomid");
List<AllUserInfor> userInfor = mySQLiteAdapter.getAllInfor(id);
for (AllUserInfor aui : userInfor){
tvname.setText(aui.getName());
tvgender.setText(aui.getGender());
}
break;
}
}
这是我从第一项活动中获取身份的方式
lvroom.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// TODO Auto-generated method stub
cursor = (Cursor) lvroom.getItemAtPosition(position);
int item_id = cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.KEY_ID));
Intent i = new Intent();
Bundle bundle = new Bundle();
bundle.putInt("Roomid", item_id);
i.putExtras(bundle);
i.setClass(FirstActivity.this, SecondActivity.class);
startActivityForResult(i, 0);
}
在我点击显示按钮inf第二个活动后,没有任何变化,这意味着名称和性别未在第二个布局中显示。我的错误在哪里?帮帮我
这是我的logcat
03-10 18:01:20.790: E/AndroidRuntime(1224): FATAL EXCEPTION: main
03-10 18:01:20.790: E/AndroidRuntime(1224): java.lang.NullPointerException
03-10 18:01:20.790: E/AndroidRuntime(1224): at com.superman.medreport.SecondActivity.onClick(SecondActivity.java:127)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.view.View.performClick(View.java:4204)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.view.View$PerformClick.run(View.java:17355)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.os.Handler.handleCallback(Handler.java:725)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.os.Handler.dispatchMessage(Handler.java:92)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.os.Looper.loop(Looper.java:137)
03-10 18:01:20.790: E/AndroidRuntime(1224): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-10 18:01:20.790: E/AndroidRuntime(1224): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 18:01:20.790: E/AndroidRuntime(1224): at java.lang.reflect.Method.invoke(Method.java:511)
03-10 18:01:20.790: E/AndroidRuntime(1224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-10 18:01:20.790: E/AndroidRuntime(1224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-10 18:01:20.790: E/AndroidRuntime(1224): at dalvik.system.NativeStart.main(Native Method)
03-10 18:01:24.802: E/Trace(1256): error opening trace file: No such file or directory (2)
答案 0 :(得分:2)
是因为_id是一个整数吗?你正在传递一个字符串
'"+id+"'
应该是
"+id+"
答案 1 :(得分:1)
请勿将id
值转换为String
中Integer
的{{1}}。
更改以下条件以获取价值:
"SELECT Name, Gender FROM MY_TABLE where _id = "+id+" ";
只需删除单个" ' "
并写为"+id+"
而不是'"+id+"'
答案 2 :(得分:0)
使用它:
"SELECT Name, Gender FROM MY_TABLE where _id ="+id+";
而不是
"SELECT Name, Gender FROM MY_TABLE where _id = '"+id+"' ";