【问题标题】:Unfortunately XXX has stopped. How to solve this?不幸的是,XXX 已停止。如何解决这个问题?
【发布时间】:2015-07-31 05:54:04
【问题描述】:

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

这是日志猫:

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)

如何发现问题? (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);
        }

)

问题解决了。

【问题讨论】:

  • 发布您的完整代码,
  • 在 com.twy.rsmap.MainActivity.(MainActivity.java:173) 仔细查看以下行
  • 第173行是一个数组:
  • 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)};

标签: android crash


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      无法实例化 Activity ComponentInfo 答案 应用因NullPointer Exception而崩溃,并且发生Null 指针异常由于findViewById() 检查中使用的XML ID 和Java code id findviewById。检查 MainActivity Line No 173

      【讨论】:

        【解决方案4】:

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

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

        )

        问题解决了。

        【讨论】:

          猜你喜欢
          • 2022-01-15
          • 2019-04-23
          • 2013-09-22
          相关资源
          最近更新 更多