Ogen
Ogen

Reputation: 6709

Android what needs to go in the onCreate method

I have this code in my main activity class

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);

And it is not working, here is the 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)

I used to have my code working when I had put these three lines in the onCreate method but the reason I changed it is that I need to access these variables inside listeners so I need them to be class variables.

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

Any help would be appreciated, thanks.

Upvotes: 0

Views: 278

Answers (3)

minhaz
minhaz

Reputation: 4233

Activity do not a have a public constructor. On the inheritance line Activity got getApplicationContext() from ContextThemeWrapper and ContextThemeWrapper default constructor is defined as

public ContextThemeWrapper() {
         super(null);
     }

And, on Android documentation Context is defined as

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

This also means that on Activity onCreate() method Android system provide the getApplicationContext() implementation. so if you call on getApplicationContext() before you will get an NPE.

And, to avoid this just create a global reference.

Upvotes: 1

LuckyMe
LuckyMe

Reputation: 3910

Change this:

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

To this:

DatabaseHandler db;
List<Contact> contactList;

And add this to onCreate():

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

Cheers.

Upvotes: 5

mawalker
mawalker

Reputation: 2070

Make sure the activity is in your manifest file, and has proper permissions

Here is another person having the same problem: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

Here is example code: (You will have to adjust the 'path to activity' to correctly match your file, and the label.

<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>

Upvotes: 1

Related Questions