不幸的是,XXX 已经停止。如何解决这个问题?

Unfortunately XXX has stopped. How to solve this?

在模拟器中启动应用程序时应用程序停止。 我找不到任何问题。

这是 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.access0(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.access0(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)

如何发现问题? (MainActivity.java:173 和 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)};

之前还可以,做完之后就出事了。 可能跟版本有关?

对象数组不太好,所以我替换后

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)};

来自

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};

并将代码替换为 (Button)findViewById(int[index])

(来自

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

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

)

问题已解决。

它清楚地表明你这里有问题。

            Caused by: java.lang.NullPointerException
            at android.app.Activity.findViewById(Activity.java:1794)
            at com.twy.rsmap.MainActivity.<init>(MainActivity.java:173)

所以,这是一个错误堆栈跟踪。它向您显示应用程序崩溃时调用了哪些内容。当查看其中一个时,找到对您自己的应用程序的引用(而不是像大多数行那样的 Android OS)并从那里开始通常是最有用的。在您的示例中,我看到:

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

因此查看 MainActivity 的第 173 行是否存在空指针异常。

无法实例化activityComponentInfo 答案 由于 NullPointer Exception 应用程序崩溃并发生空指针异常 由于 findViewById() 检查 XML ID 和Java 在 findviewById 中使用的代码 ID。检查 MainActivity 第 173 行

对象数组不太好,所以我替换后

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)};

来自

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};

并将代码替换为 (Button)findViewById(int[index])

(来自

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

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

)

问题已解决。