我有一个公共int []图像资源ID:
public static int [] prgmImages={R.drawable.im1,R.drawable.im2,R.drawable.im3,R.drawable.im4,R.drawable.im5,R.drawable.im6,R.drawable.im7,R.drawable.im8,R.drawable.im9,R.drawable.im10,R.drawable.im11,R.drawable.im12,R.drawable.im13,R.drawable.im14};
我为“public static int [] prgmImages”做了等价,用“load()”:
int[] prgmImages =load(null);
private int[] load (int[] x)
{
for (int i = 0; i <= 14; i++) {
x[i]= getResources().getIdentifier("im"+i+1, "drawable", getPackageName());
}
return x ;
}
但是我收到了错误! 问题出在哪里?
05-02 17:17:01.666:E / AndroidRuntime(18971):致命异常:主要 05-02 17:17:01.666:E / AndroidRuntime(18971):java.lang.RuntimeException:无法启动活动ComponentInfo {ir.shahid.varamin / ir.shahid.varamin.list}:java.lang.NullPointerException 05-02 17:17:01.666:E / AndroidRuntime(18971):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):在android.app.ActivityThread.access $ 600(ActivityThread.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):在android.os.Handler.dispatchMessage(Handler.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):在android.os.Looper.loop(Looper.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):在android.app.ActivityThread.main(ActivityThread.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):at java.lang.reflect.Method.invokeNative(Native Method) 05-02 17:17:01.666:E / AndroidRuntime(18971):at java.lang.reflect.Method.invoke(Method.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):at dalvik.system.NativeStart.main(Native Method) 05-02 17:17:01.666:E / AndroidRuntime(18971):引起:java.lang.NullPointerException 05-02 17:17:01.666:E / AndroidRuntime(18971):at ir.shahid.varamin.list.load(list.java:146) 05-02 17:17:01.666:E / AndroidRuntime(18971):at ir.shahid.varamin.list.onCreate(list.java:86) 05-02 17:17:01.666:E / AndroidRuntime(18971):在android.app.Activity.performCreate(Activity.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java) 05-02 17:17:01.666:E / AndroidRuntime(18971):... 12更多
答案 0 :(得分:0)
将null传递给load方法时,出现错误就不足为奇了。
为什么不在load()方法中实例化数组,例如int[] x = new int[15]
,并使方法无参数。