0

I've this issue when I run my code (caused by an intent):

08-28 05:30:11.785: E/AndroidRuntime(27529): FATAL EXCEPTION: main 08-28 05:30:11.785: E/AndroidRuntime(27529): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.tnf.timetolunch/com.tnf.timetolunch.RestaurantListActivity}: java.lang.NullPointerException 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1618) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.ActivityThread.access$1500(ActivityThread.java:124) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.os.Handler.dispatchMessage(Handler.java:99) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.os.Looper.loop(Looper.java:130) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.ActivityThread.main(ActivityThread.java:3806) 08-28 05:30:11.785: E/AndroidRuntime(27529): at java.lang.reflect.Method.invokeNative(Native Method) 08-28 05:30:11.785: E/AndroidRuntime(27529): at java.lang.reflect.Method.invoke(Method.java:507) 08-28 05:30:11.785: E/AndroidRuntime(27529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-28 05:30:11.785: E/AndroidRuntime(27529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-28 05:30:11.785: E/AndroidRuntime(27529): at dalvik.system.NativeStart.main(Native Method) 08-28 05:30:11.785: E/AndroidRuntime(27529): Caused by: java.lang.NullPointerException 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.Activity.getLocalClassName(Activity.java:3558) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.Activity.getPreferences(Activity.java:3592) 08-28 05:30:11.785: E/AndroidRuntime(27529): at com.tnf.timetolunch.RestaurantListActivity.(RestaurantListActivity.java:18) 08-28 05:30:11.785: E/AndroidRuntime(27529): at java.lang.Class.newInstanceImpl(Native Method) 08-28 05:30:11.785: E/AndroidRuntime(27529): at java.lang.Class.newInstance(Class.java:1409) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1610) 08-28 05:30:11.785: E/AndroidRuntime(27529): ... 11 more

I tried to lookup to similar errors to see how others fixed their issues, my Manifest seems OK (or at least my activity is registered):

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tnf.timetolunch"
    android:versionCode="1"
    android:versionName="@string/app_name" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:name="App"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".RestaurantListActivity"
            android:label="@string/title_activity_restaurants" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And the code from my MainActivity:

Intent i = new Intent(this, RestaurantListActivity.class);
startActivity(i);

First time I post here, hope I'll be lucky. Thanks

Vivigeos
  • 3
  • 1

3 Answers3

2

The Intent looks fine. And RestaurantListActivity is being launched as expected. The problem seems to be during its initialization:

E/AndroidRuntime(27529): Caused by: java.lang.NullPointerException
08-28 05:30:11.785: E/AndroidRuntime(27529): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.Activity.getLocalClassName(Activity.java:3558)
08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.Activity.getPreferences(Activity.java:3592) 

08-28 05:30:11.785: E/AndroidRuntime(27529): at com.tnf.timetolunch.RestaurantListActivity.(RestaurantListActivity.java:18)
08-28 05:30:11.785: E/AndroidRuntime(27529): at java.lang.Class.newInstanceImpl(Native Method) 08-28 05:30:11.785: E/AndroidRuntime(27529): at java.lang.Class.newInstance(Class.java:1409) 08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-28 05:30:11.785: E/AndroidRuntime(27529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1610)
08-28 05:30:11.785: E/AndroidRuntime(27529): ... 11 more

I wonder if you have any constructors in your RestaurantListActivity class? If so, you should not do any Context-related initialization in there...

hopia
  • 4,880
  • 7
  • 32
  • 54
0

The Problem is that you have 2 activitys with the main intent there should be only one MainActivity, And I advise you to use only one Launcher Activity

B. TIger
  • 459
  • 3
  • 10
  • 27
  • The android api works this way its for your app to have a lancher Activity and a mainActivity all subsequent Activitys should be called from or other activitys called after it – B. TIger Aug 28 '12 at 04:05
  • Thank you for the quick answer, that was my problem, although it seems the line added itself in the manifest! – Vivigeos Aug 28 '12 at 04:10
  • @B.TIger no, the Android API does _not_ work that way. You can have an infinite number of Activities that respond to the main intent. The Contacts application is a great example. Both the "People" app and the "Phone" app are actually part of the Contacts application. – Chris Thompson Aug 28 '12 at 04:13
  • Just because u dont see An activity I doesn't mean it's not launched, Ive developed apps to runs as home intent and I assure you thats how it works, thats why it solved the problem – B. TIger Aug 28 '12 at 04:17
  • @B.TIger Please see https://github.com/android/platform_packages_apps_contacts/blob/master/AndroidManifest.xml for an example of multiple activities designed to respond to the `android.intent.action.MAIN` intent. – Chris Thompson Aug 28 '12 at 04:24
  • @ChrisThompson yep I have to guive it to you its possible to use multiple main activitys but you have to add other features to the intent filter but its likely not needed to built simple aplications and if you siwtch from a app to another the last oppened activity on your app will be on the stack – B. TIger Aug 30 '12 at 18:28
0

use

 <activity
        android:name=".RestaurantListActivity"
        android:label="@string/title_activity_restaurants" >           
    </activity>

instead of

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

in your MainActivity

Intent i = new Intent(MainActivity.this, RestaurantListActivity.class);
startActivity(i);
kongkea
  • 9,788
  • 12
  • 39
  • 49