我的主要活动课程中有此代码
public class HomeScreenActivity extends Activity {
private Button contactButton;
private Button groupContactButton;
private Button historyButton;
private Button optionsButton;
private ListView contactsView;
public static int selectedContactIndex = -1;
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<Contact> contactList = db.getAllContacts();
ArrayAdapter<Contact> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
contactButton = (Button) findViewById(R.id.contactButton);
contactButton.setSelected(true);
groupContactButton = (Button) findViewById(R.id.groupContactButton);
historyButton = (Button) findViewById(R.id.historyButton);
optionsButton = (Button) findViewById(R.id.optionsButton);
contactsView = (ListView) findViewById(R.id.contactsView);
// Set up contact adaptor so the contact list can be viewed in the
// homescreen
adapter = new ArrayAdapter<Contact>(this,
R.layout.home_screen_contacts_view, contactList);
contactsView.setAdapter(adapter);
adapter.setNotifyOnChange(true);
它不工作,这里是logcat:
09-28 22:02:47.179: E/AndroidRuntime(1378): FATAL EXCEPTION: main
09-28 22:02:47.179: E/AndroidRuntime(1378): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.contactmanager/com.example.contactmanager.HomeScreenActivity}: java.lang.NullPointerException
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.os.Looper.loop(Looper.java:137)
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-28 22:02:47.179: E/AndroidRuntime(1378): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 22:02:47.179: E/AndroidRuntime(1378): at java.lang.reflect.Method.invoke(Method.java:525)
09-28 22:02:47.179: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-28 22:02:47.179: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-28 22:02:47.179: E/AndroidRuntime(1378): at dalvik.system.NativeStart.main(Native Method)
09-28 22:02:47.179: E/AndroidRuntime(1378): Caused by: java.lang.NullPointerException
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:235)
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
09-28 22:02:47.179: E/AndroidRuntime(1378): at com.example.contactmanager.DatabaseHandler.getAllContacts(DatabaseHandler.java:111)
09-28 22:02:47.179: E/AndroidRuntime(1378): at com.example.contactmanager.HomeScreenActivity.<init>(HomeScreenActivity.java:27)
09-28 22:02:47.179: E/AndroidRuntime(1378): at java.lang.Class.newInstanceImpl(Native Method)
09-28 22:02:47.179: E/AndroidRuntime(1378): at java.lang.Class.newInstance(Class.java:1130)
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
09-28 22:02:47.179: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
当我将这三行放在 onCreate 方法中时,我曾经让我的代码工作,但我更改它的原因是我需要在侦听器中访问这些变量,所以我需要它们成为类变量。
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<Contact> contactList = db.getAllContacts();
ArrayAdapter<Contact> adapter;
任何帮助将不胜感激,谢谢。