9

My application throws an exception when I install the apk on an existing version.

These are the steps to reproduce my error:

  1. Install application (i.e. v1) and run it (no error found)
  2. Install the same apk or a new one (i.e. v2) with a minor update
  3. A NullReferenceException is thrown
  4. Install the same apk of #2
  5. Everything works fine

This is the exception:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.empystudio.cashflow/com.empystudio.cashflow.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.empystudio.cashflow.MainActivity.onCreate(MainActivity.java:66)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more

This is the code of MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    documentsView = (ImageView) findViewById(R.id.documents_view);
    backupView = (ImageView) findViewById(R.id.backup_view);
    restoreView = (ImageView) findViewById(R.id.restore_view);
    helpView = (ImageView) findViewById(R.id.help_view);
    versionView = (TextView) findViewById(R.id.ver_name);
    ...

Line no. 66 is

documentsView = (ImageView) findViewById(R.id.documents_view);

I tried to comment so many part of my application without success and now I have no idea on how to solve this problem.

[Edit]

Some more info:

  1. MainActivity is the launcher Activity in the Android manifest
  2. I can't catch the exception (the only stack that I have the one from the Google Play Exception management)
  3. If I comment line 66 I still have an exception

[Edit]

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/adLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical"
    android:shrinkColumns="*"
    android:stretchColumns="*" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            tools:ignore="UseCompoundDrawables" >

            <ImageView
                android:id="@+id/documents_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:adjustViewBounds="false"
                android:contentDescription="@string/title_activity_documents"
                android:gravity="center"
                android:src="@drawable/menu_documents" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="@string/title_activity_documents" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            tools:ignore="UseCompoundDrawables" >

            <ImageView
                android:id="@+id/help_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@string/menu_help"
                android:gravity="center"
                android:src="@drawable/menu_help" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="@string/menu_help" />
        </LinearLayout>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            tools:ignore="UseCompoundDrawables" >

            <ImageView
                android:id="@+id/backup_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@string/menu_backup_database"
                android:gravity="center"
                android:src="@drawable/menu_backup" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="@string/menu_backup_database" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            tools:ignore="UseCompoundDrawables" >

            <ImageView
                android:id="@+id/restore_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@string/menu_restore_database"
                android:gravity="center"
                android:src="@drawable/menu_restore" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="@string/menu_restore_database" />
        </LinearLayout>
    </TableRow>
</TableLayout>

<TextView
    android:id="@+id/ver_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:gravity="right"
    android:textAppearance="?android:attr/textAppearanceSmall" />

[Edit]

Here there are two different APK to reproduce the error:

http://www.empystudio.com/downloads/private/cashflow_1.0.64.apk

http://www.empystudio.com/downloads/private/cashflow_1.0.65.apk

To reproduce the error:

  1. Install ver. 64
  2. Open it
  3. Install ver. 65
  4. Open it (here I receive an error)
  5. Install ver. 65 again
  6. Everything works fine
pelumi
  • 1,530
  • 12
  • 21
EmPy
  • 91
  • 3
  • 1
    So, did you change anything about the layout? – Stefan de Bruijn Apr 25 '13 at 11:49
  • No change. But if install again the same apk no error is thrown. No difference if I restart the device. – EmPy Apr 25 '13 at 12:05
  • And how th "findViewById" can throw a NullPointerException? – EmPy Apr 25 '13 at 12:08
  • could You post Your activity_main.xml? – Opiatefuchs Apr 25 '13 at 12:40
  • Posted the activity_main.xml content in the main post. My frustation is that if I install again the same application, everything works. – EmPy Apr 25 '13 at 12:52
  • 1
    findViewById is implemented as "return getWindow().findViewById(id);" so if the getWindow() returns a null, it should be a nullpointerexception. When you update a package, Android will stop your application first and then perform the install. However, I think it is difficult to determine what's going wrong with the information you provided. Can you give a link of your two apks? – StarPinkER Apr 25 '13 at 13:23
  • @Jermaine Xu: Thanks for the info. I updated the main post with 2 links to download different version and reproduce the error. I tryed to check if the getWindow() is null but no good news. – EmPy Apr 25 '13 at 13:39
  • I tried your two applications in my emulator, it works fine. 1.adb install cashflow_1.0.64.apk, then open app, no error. 2. adb install -r cashflow_1.0.65.apk, then open app, no error. – StarPinkER Apr 25 '13 at 13:55
  • Damn! On my Mediacom S102 device something is wrong. And I have no idea on how to debug it – EmPy Apr 25 '13 at 14:00
  • Anyway this is a good news because only few people that download the app from the google store will have my same problem during the upgrade – EmPy Apr 25 '13 at 14:11
  • @EmPy i had the same issue did you resolve this? – Javier Apr 28 '14 at 04:17
  • make sure that in no other layout you have has a tag called `documents_view`. Android could be checking for another documents view, searching for the wrong one and then crashing when trying to access that view item, but it is not created. Just a thought. – napkinsterror Aug 01 '15 at 08:28
  • The problem may also be related to your environment or IDE if the NPE is actually related to the R files (uh oh -- and yes, this has happened to me before). Make sure you Clean -- you may also need to delete some other files. If your IDE can be updated, you probably want to update it. Which IDE are you using? – Keilaron Aug 31 '15 at 18:12
  • Try android:launchMode="singleTask" in Manifest – Alessandro Verona Sep 03 '15 at 12:46
  • can you check the import of R made in the code? It should point to your resource directory – shiladitya Sep 18 '15 at 10:04
  • log every value in your app with, if(value == null){Log.d()} – Mehrdad Faraji Sep 24 '15 at 12:07
  • Late to the party, but: findViewById isn't the issue, since the exception is thrown in onCreate. So, one of the dereferenced references in that line must be null. It can't be R since R is dereferenced successfully above; therefore, it must be R.id. – Mike Wasson Oct 16 '15 at 14:30

0 Answers0