Android需要在onCreate方法中使用什么

时间:2013-09-29 02:06:39

标签: android sqlite

我的主要活动类

中有这个代码
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;

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

3 个答案:

答案 0 :(得分:5)

改变这个:

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

对此:

DatabaseHandler db;
List<Contact> contactList;

并将其添加到onCreate()

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

干杯。

答案 1 :(得分:1)

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

以下是另一个遇到同样问题的人:java.lang.RuntimeException: Unable to instantiate 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>

答案 2 :(得分:1)

Activity不具有公共构造函数。在继承行上,ContextThemeWrapper得到getApplicationContext(),而ContextThemeWrapper的默认构造函数定义为

public ContextThemeWrapper() {
         super(null);
     }

而且,在Android文档中,Context被定义为

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

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

并且,为了避免这种情况,只需创建一个全局参考。