1

I am trying to start one activity from another on user selection from a context menu. The code for code context menu case is:

case R.id.organize:
                   Log.d(LOGTAG, "Creating intent");
            Intent editIntent = new Intent(getApplicationContext(), AddToList.class);
            editIntent.putParcelableArrayListExtra("userPackageList", userPackages);
            Log.d(LOGTAG, "Starting activity");
            startActivity(editIntent);
            return true; 

And my AndroidManifest.xml also has entries for both the activities:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.firstapp.MyPackage"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivityOne"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".ActivityTwo"
                  android:label="@string/app_name">
        </activity>
    </application>
</manifest>

The output prints both the log statements in the switch case and then crashes with the following exception:

07-18 12:32:29.516: ERROR/AndroidRuntime(601): FATAL EXCEPTION: main
07-18 12:32:29.516: ERROR/AndroidRuntime(601): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.firstapp.MyPackage/com.firstapp.MyPackage.ActivityTwo}: java.lang.NullPointerException
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.os.Looper.loop(Looper.java:123)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at java.lang.reflect.Method.invoke(Method.java:521)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at dalvik.system.NativeStart.main(Native Method)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): Caused by: java.lang.NullPointerException
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.app.Activity.setContentView(Activity.java:1647)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.app.ListActivity.ensureList(ListActivity.java:314)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.app.ListActivity.getListView(ListActivity.java:299)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at com.firstapp.MyPackage.ActivityTwo.<init>(ActivityTwo.java:23)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at java.lang.Class.newInstanceImpl(Native Method)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at java.lang.Class.newInstance(Class.java:1429)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-18 12:32:29.516: ERROR/AndroidRuntime(601):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)

The oncreate() method for the ActivityTwo is below:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    ArrayList<PackageInfo> installedPkg = new ArrayList<PackageInfo>();
    installedPkg = getIntent().getParcelableArrayListExtra ("userPackageList"); 
    addItemsToList(installedPkg);
    setContentView(R.layout.main);
}
Ojas
  • 11
  • 2
  • 3
  • Something wrong with your layout, any ideas? – Egor Jul 18 '11 at 07:40
  • "`07-18 12:32:29.516: ERROR/AndroidRuntime(601): at om.firstapp.MyPackage.ActivityTwo.(ActivityTwo.java:23)`" It looks like a problem with the field declaration (the "global" scope of your ActivityTwo class) in you're ActivityTwo. Can you post those? – Ribose Jul 18 '11 at 07:53
  • I agree with @Ribose. I would also like to request your constructor for `ActivityTwo` (if any). – dbm Jul 18 '11 at 09:01
  • I dont have any constructor for the ActivityTwo class.. Although if reqd. the class structure is as follows: `public class ActivityTwo extends ListActivity { public ListView listView = getListView(); @Override public void onCreate(Bundle savedInstanceState) { // Posted above } //Other functions follow }` – Ojas Jul 18 '11 at 09:06
  • Also the main.xml is as follows: ` ` – Ojas Jul 18 '11 at 09:13

3 Answers3

1

EDIT:

According to your notes your ActivityTwo looks something like this:

public class ActivityTwo extends ListActivity {
    public ListView listView = getListView();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<PackageInfo> installedPkg = new ArrayList<PackageInfo>();
        installedPkg = getIntent().getParcelableArrayListExtra ("userPackageList"); 
        addItemsToList(installedPkg);
        setContentView(R.layout.main);
    }

    //Other functions follow
}

I think the exception is caused by the public ListView listView = getListView(); line. Maybe you could try to rewrite your code to something like this:

public class ActivityTwo extends ListActivity {
    public ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView = getListView();
    }

    @Override
    public void onResume() {
        ArrayList<PackageInfo> installedPkg = new ArrayList<PackageInfo>();
        installedPkg = getIntent().getParcelableArrayListExtra ("userPackageList"); 
        addItemsToList(installedPkg);
    }

    //Other functions follow
}

ORIGIN:

Maybe I'm stupid or something but in your code example you're defining your edit intent as:

Intent editIntent = new Intent(getApplicationContext(), AddToList.class);

But I don't see any AddToList activity-definition in your manifest.xml (you have ActivityOne and ActivityTwo)

Also there are situations where one should be careful with getApplicationContext(). Using Application context everywhere?

Community
  • 1
  • 1
dbm
  • 10,376
  • 6
  • 44
  • 56
  • Sorry for the typo ...The intent line is : Intent editIntent = new Intent(getApplicationContext(), ActivityTwo.class); – Ojas Jul 18 '11 at 08:56
  • Can you suggest me with any alternate way of starting this activity if it is known to cause problems ?? – Ojas Jul 18 '11 at 09:08
  • From the given stack trace I think something in your constructor for `ActivityTwo` is fishy. Or if there is no constructor then some field definitions might cause the issue. Perhaps some `public static final...` definition gone berserk. Could you perhaps edit your question above, including the constructor (if any) and your class-global variable definitions? – dbm Jul 18 '11 at 09:13
  • Ah... sorry, I just noticed you've already posted notes accordingly on your question. – dbm Jul 18 '11 at 09:14
0

Just check the R.id values u r using. All the values you use must be present in the layout that you set in the setContentview

user1999099
  • 161
  • 1
  • 3
  • 8
0

hello in your manifest file add your Activity name Correctly thid tag name for communication.

Here Suppose your Activity name is AddToList.java

activity android:name=".AddToList" android:label="@string/app_name"

but in your menfest ur activity name you written : ActivityTwo so try tag in manifest file

sravan
  • 5,303
  • 1
  • 31
  • 33