0

我在开发 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);
}

原木猫

08-05 09:11:07.180: D/AndroidRuntime(1286): 关闭 VM 08-05 09:11:07.180: W/dalvikvm(1286): threadid=1: 线程退出未捕获异常 (group=0x4001d800) 08 -05 09:11:07.203: E/AndroidRuntime(1286): 致命异常: main 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): 在 java.lang.reflect.Method.invokeNative(Native Method) 08-05 09:11:07.203: E/AndroidRuntime(1286): 在 java.lang.reflect.Method.invoke(Method.java:521) 08-05 09:11:07.203: E/AndroidRuntime(1286): 在 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):在 dalvik.system.NativeStart.main(本机方法)08-05 09:11:07.203:E/AndroidRuntime(1286):引起:java.lang.ArrayIndexOutOfBoundsException 08-05 09:11:07.203: E/AndroidRuntime(1286): 在 com.example.myscratches.SampleActivity.generatingRandomIpAddress(SampleActivity.java:32) 08-05 09:11:07.203: E/AndroidRuntime(1286): 在 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 个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): 在 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 更多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): 在 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 更多generateRandomIpAddress(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 更多generateRandomIpAddress(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 更多performLaunchActivity(ActivityThread.java:2627) 08-05 09:11:07.203: E/AndroidRuntime(1286): ... 11 更多performLaunchActivity(ActivityThread.java:2627) 08-05 09:11:07.203: E/AndroidRuntime(1286): ... 11 更多

4

2 回答 2

1

您需要先进行初始化octet,然后才能开始为其元素分配值。将您的声明更改为:

int octet[] = new int[4];

或者,添加这一行:

octet = new int[4];

generatingRandomIpAddress()开始分配octet.

PS 以后,当您寻求异常帮助时,您应该发布显示异常堆栈跟踪的 logcat 输出。

于 2012-08-05T08:33:48.553 回答
0

我不确定这是否适用于 ADT,但如果我有一段 Java 代码抛出 NullPointerException 并且我必须找出它被抛出的位置,我会执行以下操作之一:

  1. 遍历堆栈跟踪
  2. 放置一个异常断点。

对于上面的#2,看看这个。如果您在设置此断点时选择“未捕获”,则在引发任何NullPointer 异常时执行应自动停止。

于 2012-08-05T17:17:51.320 回答