0

我的主要活动课程中有此代码

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;

任何帮助将不胜感激,谢谢。

4

3 回答 3

5

改变这个:

DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<Contact> contactList = db.getAllContacts();

对此:

DatabaseHandler db;
List<Contact> contactList;

并将其添加到onCreate()

db = new DatabaseHandler(getApplicationContext());
contactList = db.getAllContacts();

干杯。

于 2013-09-29T02:12:25.613 回答
1

确保活动在您的清单文件中,并且具有适当的权限

这是另一个有同样问题的人:java.lang.RuntimeException: Unable to instance activity ComponentInfo

这是示例代码:(您必须调整“活动路径”以正确匹配您的文件和标签。

<activity 
    android:label="@string/SOME STRING FOR YOUR LABEL"
    android:name=".PATH TO ACTIVITY GOES HERE.HomeScreenActivity ">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>
于 2013-09-29T02:11:46.313 回答
1

Activity 没有公共构造函数。在继承线上 ActivitygetApplicationContext()ContextThemeWrapper和 ContextThemeWrapper 得到的默认构造函数定义为

public ContextThemeWrapper() {
         super(null);
     }

并且,在 Android 文档中,上下文定义为

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system.

这也意味着onCreate()Android系统提供了对Activity方法的getApplicationContext()实现。因此,如果您在getApplicationContext()获得 NPE 之前致电。

而且,为避免这种情况,只需创建一个全局引用。

于 2013-09-29T02:28:28.130 回答