【发布时间】: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)};