JeremiahOwen
JeremiahOwen

Reputation: 58

Android Intent extras

I am completely lost on this one. I am new to Android Dev and really am stumped by this one.

I got an application that loads a listview with all the customers names and ID's I am able to get the ID of the customer. I try to pass the intent

public final static String EXTRA_MESSAGE = "VIPID";
...
Intent intent = new Intent(ManageVIPCustomers.this, Contact.class);
                intent.putExtra(EXTRA_MESSAGE, itemString);
                startActivity(intent);

ManageVIPCustomers is the sending activity and Contact is the receiving. Contacts receiving command is:

Intent intent = getIntent();
        String VIPString = intent.getStringExtra(ManageVIPCustomers.EXTRA_MESSAGE);

Everything Compiles fine, but upon click it says the app has been stopped.

My (partial)Manifest is below:

    <activity
            android:name=".ManageVIPCustomers"
            android:label="@string/title_activity_manage_vipcustomers" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity
            android:name=".Contact"
            android:label="@string/title_activity_contact" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

Any help would be great

EDIT: LogCat below:

11-03 01:33:03.719: E/AndroidRuntime(1904): FATAL EXCEPTION: main
11-03 01:33:03.719: E/AndroidRuntime(1904): Process: edu.gatech.seclass.project2, PID: 1904
11-03 01:33:03.719: E/AndroidRuntime(1904): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{edu.gatech.seclass.project2/edu.gatech.seclass.project2.Contact}: java.lang.NullPointerException
11-03 01:33:03.719: E/AndroidRuntime(1904):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at android.os.Looper.loop(Looper.java:136)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at android.app.ActivityThread.main(ActivityThread.java:5001)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at java.lang.reflect.Method.invokeNative(Native Method)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at java.lang.reflect.Method.invoke(Method.java:515)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at dalvik.system.NativeStart.main(Native Method)
11-03 01:33:03.719: E/AndroidRuntime(1904): Caused by: java.lang.NullPointerException
11-03 01:33:03.719: E/AndroidRuntime(1904):     at android.app.Activity.findViewById(Activity.java:1884)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at edu.gatech.seclass.project2.Contact.<init>(Contact.java:29)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at java.lang.Class.newInstanceImpl(Native Method)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at java.lang.Class.newInstance(Class.java:1208)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
11-03 01:33:03.719: E/AndroidRuntime(1904):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
11-03 01:33:03.719: E/AndroidRuntime(1904):     ... 11 more

Upvotes: 0

Views: 78

Answers (1)

Karakuri
Karakuri

Reputation: 38595

This has nothing to do with the Intent or its extras. Your second Activity is calling findViewById in its constructor before you have set the content view for the Activity, probably because you are declaring something like this outside of any method:

View someView = findViewById(R.id.some_id);

Assignments outside of constructors or methods are run at the time the object is created. This is not the correct time to call findViewById. You need to declare the view only and call findViewById AFTER you have set the content view:

View someView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.some_layout);
    someView = findViewById(R.id.some_id);
    ...
}

Upvotes: 2

Related Questions