我是非常新的开发Android应用程序..当我在模拟器中运行我的程序它强制关闭..我弄清楚,我需要设置nullpointerexception但我不知道在哪里设置那些东西..请帮助我。
公共类SampleActivity扩展了Activity {
TextView dp_octet1, dp_octet2, dp_octet3, dp_octet4, dp_slashes;
int i = 0;
int j = 0;
int octet[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
iniTextView();
generatingRandomIpAddress();
}
private void generatingRandomIpAddress() {
Random i = new Random(); //Random number between 0-2
if(i.nextInt(3)==0){
octet[0] = 8+(i.nextInt(22));
octet[1] = 1+(i.nextInt(126));
}
if(i.nextInt(3)==1){
octet[0] = 16+(i.nextInt(14));
octet[1] = 128+(i.nextInt(63));
}
if(i.nextInt(3)==2){
octet[0] = 24+(i.nextInt(6));
octet[1] = 192+(i.nextInt(31));
}
octet[2] = (i.nextInt(255));
octet[3] = (i.nextInt(255));
octet[4] = (i.nextInt(255));
//Display the problem
dp_slashes.setText(octet[0]);
dp_octet1.setText(octet[1]);
dp_octet2.setText(octet[2]);
dp_octet3.setText(octet[3]);
dp_octet4.setText(octet[4]);
}
private void iniTextView() {
// TODO Auto-generated method stub
dp_octet1 = (TextView) findViewById(R.id.octet1);
dp_octet2 = (TextView) findViewById(R.id.octet2);
dp_octet3 = (TextView) findViewById(R.id.octet3);
dp_octet4 = (TextView) findViewById(R.id.octet4);
dp_slashes = (TextView) findViewById(R.id.slashes);
}
log cat
08-05 09:11:07.180:D / AndroidRuntime(1286):关闭虚拟机 08-05 09:11:07.180:W / dalvikvm(1286):threadid = 1:线程退出未捕获异常(组= 0x4001d800) 08-05 09:11:07.203:E / AndroidRuntime(1286):致命异常:主要 08-05 09:11:07.203:E / AndroidRuntime(1286):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.myscratches / com.example.myscratches.SampleActivity}:java.lang.ArrayIndexOutOfBoundsException 08-05 09:11:07.203:E / AndroidRuntime(1286):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 08-05 09:11:07.203:E / AndroidRuntime(1286):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 08-05 09:11:07.203:E / AndroidRuntime(1286):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125) 08-05 09:11:07.203:E / AndroidRuntime(1286):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 08-05 09:11:07.203:E / AndroidRuntime(1286):在android.os.Handler.dispatchMessage(Handler.java:99) 08-05 09:11:07.203:E / AndroidRuntime(1286):在android.os.Looper.loop(Looper.java:123) 08-05 09:11:07.203:E / AndroidRuntime(1286):在android.app.ActivityThread.main(ActivityThread.java:4627) 08-05 09:11:07.203:E / AndroidRuntime(1286):at java.lang.reflect.Method.invokeNative(Native Method) 08-05 09:11:07.203:E / AndroidRuntime(1286):at java.lang.reflect.Method.invoke(Method.java:521) 08-05 09:11:07.203:E / AndroidRuntime(1286):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 08-05 09:11:07.203:E / AndroidRuntime(1286):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-05 09:11:07.203:E / AndroidRuntime(1286):at dalvik.system.NativeStart.main(Native Method) 08-05 09:11:07.203:E / AndroidRuntime(1286):引起:java.lang.ArrayIndexOutOfBoundsException 08-05 09:11:07.203:E / AndroidRuntime(1286):at com.example.myscratches.SampleActivity.generatingRandomIpAddress(SampleActivity.java:32) 08-05 09:11:07.203:E / AndroidRuntime(1286):at com.example.myscratches.SampleActivity.onCreate(SampleActivity.java:24) 08-05 09:11:07.203:E / AndroidRuntime(1286):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-05 09:11:07.203:E / AndroidRuntime(1286):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 08-05 09:11:07.203:E / AndroidRuntime(1286):... 11 more
答案 0 :(得分:1)
您需要先初始化octet
,然后才能开始为其元素分配值。将您的声明更改为:
int octet[] = new int[4];
或者,添加以下行:
octet = new int[4];
在开始分配generatingRandomIpAddress()
的元素之前,到octet
。
P.S。将来,当您在寻求异常帮助时,应该发布logcat输出,显示异常的堆栈跟踪。
答案 1 :(得分:0)
我不确定这是否适用于ADT,但如果我有一段Java代码,其中抛出了NullPointerException并且我必须找出它被抛出的位置,我会做以下其中一项: / p>
对于上面的#2,请查看this。如果在设置此断点时选择“未捕获”,则在抛出任何 NullPointer异常时,执行应自动停止。