-4

The app is stopped when starting the app in emulator. I can't find any problems.

Here's The logcat:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.twy.rsmap/com.twy.rsmap.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
        at android.app.ActivityThread.access$600(ActivityThread.java:122)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4340)
        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:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.app.Activity.findViewById(Activity.java:1794)
        at com.twy.rsmap.MainActivity.<init>(MainActivity.java:173)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1319)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            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:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

How to find out the problem? (MainActivity.java:173 and MainActivity.java:174)

Button[] buttons = new Button[]{(Button)findViewById(R.id.FileToolStripMenuItem),(Button)findViewById(R.id.ImageToolStripMenuItem),(Button)findViewById(R.id.GroundToolStripMenuItem),(Button)findViewById(R.id.SeaToolStripMenuItem),(Button)findViewById(R.id.NailToolStripMenuItem),(Button)findViewById(R.id.RubberToolStripMenuItem),(Button)findViewById(R.id.SpeedboardToolStripMenuItem),(Button)findViewById(R.id.ItemToolStripMenuItem)};
LinearLayout[] lls = new LinearLayout[]{(LinearLayout)findViewById(R.id.FileDrop),(LinearLayout)findViewById(R.id.ImageDrop),(LinearLayout)findViewById(R.id.GroundDrop),(LinearLayout)findViewById(R.id.SeaDrop),(LinearLayout)findViewById(R.id.NailDrop),(LinearLayout)findViewById(R.id.RubberDrop),(LinearLayout)findViewById(R.id.SpeedDrop),(LinearLayout)findViewById(R.id.ItemDrop)};

This works properly before, but something happened after doing something. Maybe related to the version?

The object array is not so well, so after I replaced

Button[] buttons = new Button[]{(Button)findViewById(R.id.FileToolStripMenuItem),(Button)findViewById(R.id.ImageToolStripMenuItem),(Button)findViewById(R.id.GroundToolStripMenuItem),(Button)findViewById(R.id.SeaToolStripMenuItem),(Button)findViewById(R.id.NailToolStripMenuItem),(Button)findViewById(R.id.RubberToolStripMenuItem),(Button)findViewById(R.id.SpeedboardToolStripMenuItem),(Button)findViewById(R.id.ItemToolStripMenuItem)};
LinearLayout[] lls = new LinearLayout[]{(LinearLayout)findViewById(R.id.FileDrop),(LinearLayout)findViewById(R.id.ImageDrop),(LinearLayout)findViewById(R.id.GroundDrop),(LinearLayout)findViewById(R.id.SeaDrop),(LinearLayout)findViewById(R.id.NailDrop),(LinearLayout)findViewById(R.id.RubberDrop),(LinearLayout)findViewById(R.id.SpeedDrop),(LinearLayout)findViewById(R.id.ItemDrop)};

by

int[] idb = {R.id.FileToolStripMenuItem,R.id.ImageToolStripMenuItem,R.id.GroundToolStripMenuItem,R.id.SeaToolStripMenuItem,R.id.NailToolStripMenuItem,R.id.RubberToolStripMenuItem,R.id.SpeedboardToolStripMenuItem,R.id.ItemToolStripMenuItem};
int[] idl = {R.id.FileDrop,R.id.ImageDrop,R.id.GroundDrop,R.id.SeaDrop,R.id.NailDrop,R.id.RubberDrop,R.id.SpeedDrop,R.id.ItemDrop};

And replaced the codes with (Button)findViewById(int[index])

(From

        int a ;
        for (a=0;a<buttons.length;a++){
            buttons[a].setVisibility(View.VISIBLE);
        }

to

        int a ;
        for (a=0;a<idb.length;a++){
            Button button1 = (Button)findViewById(idb[a]);
            button1.setVisibility(View.VISIBLE);
        }

)

Problem solved.

  • post your full code, – Kumar Jul 31 '15 at 05:57
  • possible duplicate of [Unfortunately Project_Name Has Stopped](http://stackoverflow.com/questions/11441120/unfortunately-project-name-has-stopped) – Umair M Jul 31 '15 at 05:57
  • see the following line carefully at, com.twy.rsmap.MainActivity.(MainActivity.java:173) – Reprator Jul 31 '15 at 06:23
  • The 173th row is an array : – WingYin Tang Jul 31 '15 at 10:06
  • Button[] buttons = {(Button)findViewById(R.id.FileToolStripMenuItem),(Button)findViewById(R.id.ImageToolStripMenuItem),(Button)findViewById(R.id.GroundToolStripMenuItem),(Button)findViewById(R.id.SeaToolStripMenuItem),(Button)findViewById(R.id.NailToolStripMenuItem),(Button)findViewById(R.id.RubberToolStripMenuItem),(Button)findViewById(R.id.SpeedboardToolStripMenuItem),(Button)findViewById(R.id.ItemToolStripMenuItem)}; – WingYin Tang Jul 31 '15 at 10:06
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Peter O. Aug 01 '15 at 02:46
  • Actually the real problem is about array and findViewById. – WingYin Tang Aug 03 '15 at 12:46
  • There is same problem with you, you can look this problem,http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – HenryTien May 10 '16 at 22:02

4 Answers4

1

its saying clearly you have issue here.

            Caused by: java.lang.NullPointerException
            at android.app.Activity.findViewById(Activity.java:1794)
            at com.twy.rsmap.MainActivity.<init>(MainActivity.java:173)
Sohail Zahid
  • 7,738
  • 2
  • 22
  • 34
1

So, that's an error stack trace. It shows you what stuff was called when the app crashed. When looking at one of these its often most useful to find a reference to your own app (rather than the Android OS as most lines are) and go from there. In your example I see:

    at com.twy.rsmap.MainActivity.<init>(MainActivity.java:173)

So look on MainActivity's line 173 for a null pointer exception.

JMRboosties
  • 14,480
  • 18
  • 68
  • 113
0

Unable to instantiate activity ComponentInfo Ans The App is crashed due to the NullPointer Exception & Null pointer exception is occur due to findViewById() check the XML ID and the Java code id which are used in findviewById. Check the MainActivity Line No 173

Maheshwar Ligade
  • 6,171
  • 3
  • 35
  • 53
0

The object array is not so well, so after I replaced

Button[] buttons = new Button[]{(Button)findViewById(R.id.FileToolStripMenuItem),(Button)findViewById(R.id.ImageToolStripMenuItem),(Button)findViewById(R.id.GroundToolStripMenuItem),(Button)findViewById(R.id.SeaToolStripMenuItem),(Button)findViewById(R.id.NailToolStripMenuItem),(Button)findViewById(R.id.RubberToolStripMenuItem),(Button)findViewById(R.id.SpeedboardToolStripMenuItem),(Button)findViewById(R.id.ItemToolStripMenuItem)};
LinearLayout[] lls = new LinearLayout[]{(LinearLayout)findViewById(R.id.FileDrop),(LinearLayout)findViewById(R.id.ImageDrop),(LinearLayout)findViewById(R.id.GroundDrop),(LinearLayout)findViewById(R.id.SeaDrop),(LinearLayout)findViewById(R.id.NailDrop),(LinearLayout)findViewById(R.id.RubberDrop),(LinearLayout)findViewById(R.id.SpeedDrop),(LinearLayout)findViewById(R.id.ItemDrop)};

by

int[] idb = {R.id.FileToolStripMenuItem,R.id.ImageToolStripMenuItem,R.id.GroundToolStripMenuItem,R.id.SeaToolStripMenuItem,R.id.NailToolStripMenuItem,R.id.RubberToolStripMenuItem,R.id.SpeedboardToolStripMenuItem,R.id.ItemToolStripMenuItem};
int[] idl = {R.id.FileDrop,R.id.ImageDrop,R.id.GroundDrop,R.id.SeaDrop,R.id.NailDrop,R.id.RubberDrop,R.id.SpeedDrop,R.id.ItemDrop};

And replaced the codes with (Button)findViewById(int[index])

(From

        int a ;
        for (a=0;a<buttons.length;a++){
            buttons[a].setVisibility(View.VISIBLE);
        }

to

        int a ;
        for (a=0;a<idb.length;a++){
            Button button1 = (Button)findViewById(idb[a]);
            button1.setVisibility(View.VISIBLE);
        }

)

Problem solved.