在模拟器中启动应用程序时停止应用程序。 我找不到任何问题。
这是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)
如何找出问题? (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);
}
)
问题解决了。
答案 0 :(得分:1)
它的说法显然你在这里有问题。
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1794)
at com.twy.rsmap.MainActivity.<init>(MainActivity.java:173)
答案 1 :(得分:1)
所以,这是一个错误堆栈跟踪。它会显示应用程序崩溃时调用的内容。在查看其中一个时,通常最有用的是找到对您自己的应用程序的引用(而不是大多数行的Android操作系统)并从那里开始。在你的例子中,我看到:
at com.twy.rsmap.MainActivity.<init>(MainActivity.java:173)
因此,请查看MainActivity的第173行,了解空指针异常。
答案 2 :(得分:0)
无法实例化活动ComponentInfo 答 由于 NullPointer异常&amp;由于findViewById()检查 findviewById 中使用的XML ID和Java代码ID,因此出现空指针异常。检查 MainActivity Line No 173
答案 3 :(得分:0)
对象数组不太好,所以在我替换
之后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);
}
)
问题解决了。