请不要向我扔石头,但我无法找到答案为什么我无法从MainActivity中的类DB调用方法... 好像我没有做错什么,但...... 我每次尝试NullPointerException,我做错了什么? 谢谢你的回答!
public class MainActivity extends ActionBarActivity{
SQLiteDatabase db;
DBHelper dbHelper;
DB mDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DBHelper(this);
db = dbHelper.getReadableDatabase();
mDB = new DB();
mDB.fillListView();
}
和方法
的第二课public class DB {
final String LOG = "phonebookLogs";
private static final String TABLE_NAME = "contacts";
private static final String COL_NAME = "name";
private static final String COL_SURNAME = "surname";
private static final String COL_BIRTHDAY = "birthday";
private static final String COL_SEX = "sex";
private static final String COL_ADRESS = "adress";
private static final String COL_LINK = "link";
private static final String ID = "_id";
Cursor c;
public void fillListView(){
c = db.query(TABLE_NAME, null, null, null, null, null, null);
if(c != null){
if(c.moveToFirst()){
do{
String[] names = {c.getString(c.getColumnIndex(COL_SURNAME)) + " "
+ c.getString(c.getColumnIndex(COL_NAME))};
int[] links = {c.getInt(c.getColumnIndex(COL_LINK))};
if(links[0] == 0){
links[0] = R.drawable.default_contact;
}
Log.d(LOG, "\n" + links[0] + " " + names[0]);
}while(c.moveToNext());
}
}else{
Log.d(LOG, "Cursor is null!");
}
}
}
日志
07-06 23:37:09.929: E/AndroidRuntime(10952): FATAL EXCEPTION: main
07-06 23:37:09.929: E/AndroidRuntime(10952): Process: com.nixsolutions.phonebook, PID: 10952
07-06 23:37:09.929: E/AndroidRuntime(10952): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nixsolutions.phonebook/com.nixsolutions.phonebook.MainActivity}: java.lang.NullPointerException
07-06 23:37:09.929: E/AndroidRuntime(10952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
07-06 23:37:09.929: E/AndroidRuntime(10952): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
07-06 23:37:09.929: E/AndroidRuntime(10952): at android.app.ActivityThread.access$800(ActivityThread.java:139)
07-06 23:37:09.929: E/AndroidRuntime(10952): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
07-06 23:37:09.929: E/AndroidRuntime(10952): at android.os.Handler.dispatchMessage(Handler.java:102)
07-06 23:37:09.929: E/AndroidRuntime(10952): at android.os.Looper.loop(Looper.java:136)
07-06 23:37:09.929: E/AndroidRuntime(10952): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-06 23:37:09.929: E/AndroidRuntime(10952): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 23:37:09.929: E/AndroidRuntime(10952): at java.lang.reflect.Method.invoke(Method.java:515)
07-06 23:37:09.929: E/AndroidRuntime(10952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-06 23:37:09.929: E/AndroidRuntime(10952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
07-06 23:37:09.929: E/AndroidRuntime(10952): at dalvik.system.NativeStart.main(Native Method)
07-06 23:37:09.929: E/AndroidRuntime(10952): Caused by: java.lang.NullPointerException
07-06 23:37:09.929: E/AndroidRuntime(10952): at com.nixsolutions.phonebook.DB.fillListView(DB.java:26)
07-06 23:37:09.929: E/AndroidRuntime(10952): at com.nixsolutions.phonebook.MainActivity.onCreate(MainActivity.java:63)
07-06 23:37:09.929: E/AndroidRuntime(10952): at android.app.Activity.performCreate(Activity.java:5275)
07-06 23:37:09.929: E/AndroidRuntime(10952): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-06 23:37:09.929: E/AndroidRuntime(10952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
07-06 23:37:09.929: E/AndroidRuntime(10952): ... 11 more
答案 0 :(得分:0)
创建DB类的dbHelper和db成员
更改fillListView
public void fillListView(Context context){
dbHelper = new DBHelper(context);
db = dbHelper.getReadableDatabase();
c = db.query(TABLE_NAME, null, null, null, null, null, null);
将其命名为
mDB.fillListView(this)