检索数据时出现Java NullPointerException

时间:2015-02-13 13:21:05

标签: java android eclipse sqlite

我一直在尝试从预加载的数据库中检索数据,并且我总是收到NullPointerException的错误。我试图将数据检索到列表视图。我不知道问题所在。请帮我。感谢。

public class Retrieve extends Activity {  
Dbhelper dbhelper;  
protected ListAdapter adapter;
SQLiteDatabase db;  
@Override  
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.ret_all_info);    
String[] from = new String[] { "_id" };  
int[] to = new int[] { R.id.studid};  
dbhelper = new Dbhelper(this);  
try {  
dbhelper.createDataBase();  
} catch (IOException e) {  
 // TODO Auto-generated catch block  
e.printStackTrace();  
}  

Cursor c = dbhelper.getData();  

adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.ret_all, c, from, to, 0);  

ListView list = (ListView) findViewById(R.id.ListView1);  

list.setAdapter(adapter);  

list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub

Intent intent = new Intent(Retrieve.this, RetrieveDetails.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("COMMAND_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);


}

});
}  
}  

这是Logcat

02-13 21:05:11.402: E/Trace(22808): error opening trace file: No such file or directory (2)
02-13 21:05:11.572: E/dalvikvm(22808): GC_CONCURRENT freed 314K, 9% free 6590K/7239K, paused 2ms+12ms, total 28ms
02-13 21:05:22.832: E/Trace(22895): error opening trace file: No such file or directory (2)
02-13 21:05:22.972: E/dalvikvm(22895): GC_CONCURRENT freed 305K, 10% free 6577K/7239K, paused 13ms+3ms, total 37ms
02-13 21:05:23.192: E/AndroidRuntime(22895): FATAL EXCEPTION: main
02-13 21:05:23.192: E/AndroidRuntime(22895): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutsiroll/com.example.tutsiroll.Retrieve}: java.lang.NullPointerException
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.ActivityThread.access$600(ActivityThread.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.os.Handler.dispatchMessage(Handler.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.os.Looper.loop(Looper.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.ActivityThread.main(ActivityThread.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at java.lang.reflect.Method.invokeNative(Native Method) 
02-13 21:05:23.192: E/AndroidRuntime(22895):    at java.lang.reflect.Method.invoke(Method.java:511) 
02-13 21:05:23.192: E/AndroidRuntime(22895):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at dalvik.system.NativeStart.main(Native Method)
02-13 21:05:23.192: E/AndroidRuntime(22895): Caused by: java.lang.NullPointerException
02-13 21:05:23.192: E/AndroidRuntime(22895):    at com.example.tutsiroll.Retrieve.onCreate(Retrieve.java:43)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.Activity.performCreate(Activity.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at com.lbe.security.service.core.client.b.x.callActivityOnCreate(Unknown Source)
02-13 21:05:23.192: E/AndroidRuntime(22895):    ... 12 more

1 个答案:

答案 0 :(得分:0)

您表示NPE出现在这里:

   ListView list = (ListView) findViewById(R.id.ListView1);  
-> list.setAdapter(adapter);  

这意味着list为null,可能是因为R.layout.ret_all_info中没有id为R.id.ListView1的视图。