Cursor上的Nullpointer异常 它在mainactivity中成功检索数据库中的值。 但它在DepartmentDesignation.class文件中显示空指针异常。上
HODcursor = mydb.get_dept_list() ;
在DepartmentDesignation.java中
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends Activity {
private MyDBHelper mydb;
private SimpleCursorAdapter adapter;
public static final String Row_ID = "row_id";
ListView dplist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//********************************//
//Begin of Database-Table1_Dept_List
//********************************//
dplist= (ListView) findViewById(R.id.list_view);
//Instantiate table1_dept_list
mydb =new MyDBHelper(this, null, null,4);
SQLiteDatabase db =mydb.getReadableDatabase();
//Removing duplicate values from table1_dept_list
mydb.delete_dep_list();
//Inserting into table1_dept_list
mydb.add_dept_list("DLBIOCHEM","BioChemistry");
mydb.add_dept_list("DLBIOTECH","BioTechnology");
mydb.add_dept_list("DLBOT", "Botany");
mydb.add_dept_list("DLCHEM","Chemistry");
mydb.add_dept_list("DLCOM","Commerce");
mydb.add_dept_list("DLCS","Computer Science");
mydb.add_dept_list("DLECO","Economics");
mydb.add_dept_list("DLEDU","Education");
mydb.add_dept_list("DLENG","English");
mydb.add_dept_list("DLEVS","Environmental Science");
mydb.add_dept_list("DLFSN","Food Science and Nutrition");
mydb.add_dept_list("DLGEO","Geology");
mydb.add_dept_list("DLJMC", "Journalism and Massmedia Communication");
mydb.add_dept_list("DLLIS","Library and Information Science");
mydb.add_dept_list("DLMATH","Mathematics");
mydb.add_dept_list("DLMICRO","Microbiology");
mydb.add_dept_list("DLPE","Physical Education");
mydb.add_dept_list("DLPHY","Physics");
mydb.add_dept_list("DLPRIMS","Periyar Institute of Management Studies");
mydb.add_dept_list("DLPSY","Psychology");
mydb.add_dept_list("DLSOC","Sociology");
mydb.add_dept_list("DLTAM", "Tamil");
mydb.add_dept_list("DLTAD","Textile and Apparel Design");
mydb.add_dept_list("DLZOO","Zoology");
//list view
Cursor AllDeptList = mydb.get_dept_list();
String[] from=new String[] {
MyDBHelper.DEPT_LIST_COLUMN_NAME
};
int[] to=new int[] {R.id.dis_text};
adapter = new SimpleCursorAdapter(this,R.layout.disp_text,AllDeptList,from,to,0 );
dplist.setAdapter(adapter);
//********************************//
//end of Database-Table1_Dept_List//
//********************************//
//******************************//
//Passing values to new activity//
//******************************//
dplist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
// TODO Auto-generated method stub
Cursor cursor = (Cursor) dplist.getItemAtPosition(position);
String val = cursor.getString(cursor.getColumnIndex(MyDBHelper.DEPT_LIST_COLUMN_ID));
Intent intent_dp_list = new Intent(MainActivity.this,DepartmentDesignation.class);
intent_dp_list.putExtra("val",val);
startActivity(intent_dp_list);
}
});
//*********************************//
//end of passing values to activity//
//*********************************//
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MyDBHelper.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHelper extends SQLiteOpenHelper{
public static final String DEPT_LIST_TABLE1_NAME="tbl_dep_list";
public static final String DEPT_LIST_ROW_ID = "_id";
public static String DEPT_LIST_COLUMN_ID = "fld_tb1_id";
public static final String DEPT_LIST_COLUMN_NAME="fld_tb1_list";
public MyDBHelper(Context context, String name, CursorFactory factory,int version) {
super(context, "contact_book.db", factory, 11);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase database) {
// TODO Auto-generated method stub
database.execSQL(" CREATE TABLE " + DEPT_LIST_TABLE1_NAME + "(" + DEPT_LIST_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DEPT_LIST_COLUMN_ID + " TEXT, " + DEPT_LIST_COLUMN_NAME + " TEXT ) " );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DEPT_LIST_TABLE1_NAME);
onCreate(db);
}
public void add_dept_list(String dl_id, String dep_list) {
// TODO Auto-generated method stub
ContentValues values = new ContentValues(2);
values.put(MyDBHelper.DEPT_LIST_COLUMN_ID , dl_id);
values.put(MyDBHelper.DEPT_LIST_COLUMN_NAME,dep_list);
getWritableDatabase().insert(MyDBHelper.DEPT_LIST_TABLE1_NAME,null,values);
}
public int delete_dep_list(){
try{
SQLiteDatabase db =this.getWritableDatabase();
return db.delete(DEPT_LIST_TABLE1_NAME, null, null);
}
catch(Exception e){
e.printStackTrace();
}
return 0;
}
public Cursor get_dept_list(){
String[] from = new String[] {MyDBHelper.DEPT_LIST_ROW_ID,MyDBHelper.DEPT_LIST_COLUMN_ID,MyDBHelper.DEPT_LIST_COLUMN_NAME};
Cursor cursor = getReadableDatabase().query(MyDBHelper.DEPT_LIST_TABLE1_NAME, from,null,null,null, null, null);
if(cursor != null){
cursor.moveToFirst();
}
return cursor;
}
}
DepartmentDesignation.java
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class DepartmentDesignation extends Activity {
ListView dslist;
public static String d_id;
private SimpleCursorAdapter HODadapter;
SQLiteDatabase db;
private MyDBHelper mydb;
public static String msg;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.disp_dept_desig);
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("val");
TextView txtid = (TextView) findViewById(R.id.idtxt);
txtid.setText(message);
msg=message;
//Cursor HODcursor = db.rawQuery(q, null) ;
Cursor HODcursor = mydb.get_dept_list();
String[] HODfrom = new String[]{
MyDBHelper.DEPT_LIST_COLUMN_ID,
MyDBHelper.DEPT_LIST_COLUMN_NAME
};
int[] HODto = new int[]{
R.id.tv_hod,
R.id.tv_hodn
};
dslist = (ListView) findViewById(R.id.desig_list);
HODadapter = new SimpleCursorAdapter(this,R.layout.disp_hod,HODcursor,HODfrom,HODto,0);
dslist.setAdapter(HODadapter);
}
}
LogCat错误
10-19 09:27:45.980: W/dalvikvm(2392): threadid=1: thread exiting with uncaught exception (group=0xb4ab7b90)
10-19 09:27:46.090: E/AndroidRuntime(2392): FATAL EXCEPTION: main
10-19 09:27:46.090: E/AndroidRuntime(2392): Process: com.example.contact, PID: 2392
10-19 09:27:46.090: E/AndroidRuntime(2392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contact/com.example.contact.DepartmentDesignation}: java.lang.NullPointerException
10-19 09:27:46.090: E/AndroidRuntime(2392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
10-19 09:27:46.090: E/AndroidRuntime(2392): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
10-19 09:27:46.090: E/AndroidRuntime(2392): at android.app.ActivityThread.access$700(ActivityThread.java:135)
10-19 09:27:46.090: E/AndroidRuntime(2392): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
10-19 09:27:46.090: E/AndroidRuntime(2392): at android.os.Handler.dispatchMessage(Handler.java:102)
10-19 09:27:46.090: E/AndroidRuntime(2392): at android.os.Looper.loop(Looper.java:137)
10-19 09:27:46.090: E/AndroidRuntime(2392): at android.app.ActivityThread.main(ActivityThread.java:4998)
10-19 09:27:46.090: E/AndroidRuntime(2392): at java.lang.reflect.Method.invokeNative(Native Method)
10-19 09:27:46.090: E/AndroidRuntime(2392): at java.lang.reflect.Method.invoke(Method.java:515)
10-19 09:27:46.090: E/AndroidRuntime(2392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
10-19 09:27:46.090: E/AndroidRuntime(2392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
10-19 09:27:46.090: E/AndroidRuntime(2392): at dalvik.system.NativeStart.main(Native Method)
10-19 09:27:46.090: E/AndroidRuntime(2392): Caused by: java.lang.NullPointerException
10-19 09:27:46.090: E/AndroidRuntime(2392): at com.example.contact.DepartmentDesignation.onCreate(DepartmentDesignation.java:37)
10-19 09:27:46.090: E/AndroidRuntime(2392): at android.app.Activity.performCreate(Activity.java:5243)
10-19 09:27:46.090: E/AndroidRuntime(2392): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-19 09:27:46.090: E/AndroidRuntime(2392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
10-19 09:27:46.090: E/AndroidRuntime(2392): ... 11 more
10-19 09:27:50.980: I/Process(2392): Sending signal. PID: 2392 SIG: 9
答案 0 :(得分:0)
mydb
从未初始化为onCreate
方法。