我收到以下错误
05-08 01:03:16.003: D/dalvikvm(1147): GC_FOR_ALLOC freed 46K, 4% free 3036K/3156K, paused 98ms, total 102ms
05-08 01:03:16.313: D/dalvikvm(1147): GC_FOR_ALLOC freed 3K, 4% free 3386K/3508K, paused 42ms, total 43ms
05-08 01:03:17.263: D/gralloc_goldfish(1147): Emulator without GPU emulation detected.
05-08 01:03:21.563: D/dalvikvm(1147): GC_FOR_ALLOC freed 11K, 2% free 3850K/3928K, paused 139ms, total 146ms
05-08 01:03:21.733: D/dalvikvm(1147): GC_FOR_ALLOC freed 4K, 3% free 3944K/4028K, paused 139ms, total 140ms
05-08 01:03:21.813: I/dalvikvm-heap(1147): Grow heap (frag case) to 4.990MB for 1127536-byte allocation
05-08 01:03:22.043: D/dalvikvm(1147): GC_FOR_ALLOC freed <1K, 2% free 5045K/5132K, paused 224ms, total 224ms
05-08 01:03:23.943: D/AndroidRuntime(1147): Shutting down VM
05-08 01:03:23.943: W/dalvikvm(1147): threadid=1: thread exiting with uncaught exception (group=0xb1ad3ba8)
05-08 01:03:24.003: E/AndroidRuntime(1147): FATAL EXCEPTION: main
05-08 01:03:24.003: E/AndroidRuntime(1147): Process: com.example.crosswordapp, PID: 1147
05-08 01:03:24.003: E/AndroidRuntime(1147): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crosswordapp/com.example.crosswordapp.MainActivity}: java.lang.NullPointerException
05-08 01:03:24.003: E/AndroidRuntime(1147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-08 01:03:24.003: E/AndroidRuntime(1147): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-08 01:03:24.003: E/AndroidRuntime(1147): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-08 01:03:24.003: E/AndroidRuntime(1147): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-08 01:03:24.003: E/AndroidRuntime(1147): at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 01:03:24.003: E/AndroidRuntime(1147): at android.os.Looper.loop(Looper.java:136)
05-08 01:03:24.003: E/AndroidRuntime(1147): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-08 01:03:24.003: E/AndroidRuntime(1147): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 01:03:24.003: E/AndroidRuntime(1147): at java.lang.reflect.Method.invoke(Method.java:515)
05-08 01:03:24.003: E/AndroidRuntime(1147): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-08 01:03:24.003: E/AndroidRuntime(1147): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-08 01:03:24.003: E/AndroidRuntime(1147): at dalvik.system.NativeStart.main(Native Method)
05-08 01:03:24.003: E/AndroidRuntime(1147): Caused by: java.lang.NullPointerException
05-08 01:03:24.003: E/AndroidRuntime(1147): at com.example.crosswordapp.MainActivity.onCreate(MainActivity.java:139)
05-08 01:03:24.003: E/AndroidRuntime(1147): at android.app.Activity.performCreate(Activity.java:5231)
05-08 01:03:24.003: E/AndroidRuntime(1147): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-08 01:03:24.003: E/AndroidRuntime(1147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-08 01:03:24.003: E/AndroidRuntime(1147): ... 11 more
05-08 01:08:24.593: I/Process(1147): Sending signal. PID: 1147 SIG: 9
这是我的Log-cat
我的MainActivity.java就是这个
String output = "-,-,-,-,-,-,-,-,-,-,-,-,-,;-,a,-,c,g,-,-,-,g,-,-,-,-,;c,s,-,o,a,-,-,-,r,-,-,-,-,;o,s,-,h,t,-,-,-,o,-,-,-,-,;m,e,-,o,h,-,-,-,u,p,-,-,-,;p,m,-,r,e,-,-,-,p,e,-,-,-,;a,b,s,t,r,a,c,t,i,o,n,-,-,;n,l,-,-,i,-,-,-,n,p,-,-,-,;y,a,-,-,n,-,-,-,g,l,-,-,-,;-,g,-,-,g,-,-,-,-,e,-,-,-,;-,e,-,-,-,-,-,-,-,-,-,-,-,;-,-,-,-,-,-,-,-,-,-,-,-,-,;-,-,-,-,-,-,-,-,-,-,-,-,-,;";
String[][] populate = new String[13][13];
String[] outputRow = output.split(";");
// Log.i(outputRow+"output");
for (int i = 0; i < outputRow.length; i++) {
String[] outputCol = outputRow[i].split(",");
for (int j = 0; j < outputCol.length; j++) {
buttons[i][j] = new TextView(MainActivity.this);
buttons[i][j].setBackgroundResource(R.drawable.white);
if (populate[i][j].equalsIgnoreCase("-")) {
buttons[i][j].setText(populate[i][j]);
buttons[i][j].setBackgroundResource(R.drawable.black);
buttons[i][j].setEnabled(false);
} else {
buttons[i][j].setText(populate[i][j]);
buttons[i][j].setBackgroundResource(R.drawable.white);
buttons[i][j].setEnabled(true);
}
这里我需要显示我在程序中返回的字符串输出的网格。在这里我没有收到任何错误,但在运行应用程序时它显示:
不幸的是申请已停止。
这里有139行
if(populate [i] [j] .equalsIgnoreCase(&#34; - &#34;))
这里显示空指针异常。我不知道它为什么会来。
答案 0 :(得分:2)
虽然您已定义String[][] populate = new String[13][13];
我在你的代码中没有看到你填充它的位置
这导致populate[i][j]
为空,并且您将该异常抛出。
在使用之前尝试将值放在populate [] []中。
答案 1 :(得分:0)
您的错误表明您有一个NULL POINTER EXCEPTION,这意味着其中一个字符串没有取任何值并保持为null。因此,当您尝试访问该字符串时,会发生错误。 尝试先打印所有字符串,看看哪个字符串存储了哪些数据,也许这样就可以解决错误。