0

我创建了一个 int 值数组。这些 int 值是可绘制图像的路径 ID,如下所示:

int[] picItems = {R.drawable.pic_one, R.drawable.pic_two, R.drawable.pic_three};

但是,picItem 数组为空。我希望能够从索引中获取值并将其用作背景,如下所示:

relative.setBackgroundResource(picItems[index]);

现在,如果我使用上面的代码并将参数字段设置为 R.drawable.pic_one,例如,它可以工作。但是,每当我声明和实例化 int 数组 picItems 时,我都会得到一个空指针异常。我一定是在看一些东西并犯了一个简单的错误,但如果我是,请随时指出。我很感激任何帮助,谢谢!

编辑:我尝试了许多不同的方法来声明和实例化数组。即使在实例化它之后数组也是空的,但是在尝试使用它时遇到错误(我使用 if 语句来捕获它)。这里还有一点代码:

    public class Snow extends Activity{
        int[] picItems;
        RelativeLayout relative;
        SharedPreferences sp;
        int checkedItem = 0;

        @Override
        public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

            picItems = new int[]{R.drawable.pic_one, R.drawable.pic_two, R.drawable.pic_three};
            sp = getPreferences(MODE_PRIVATE);
            checkedItem = sp.getInt("picture", 0);
            if (!(picItems == null) || !(picItems.equals(null))){
            Toast.makeText(getApplicationContext(), "Array is null for some reason", Toast.LENGTH_SHORT).show();
        }else{
             relative.setBackgroundResource(picItems[checkedItem]);
        }//end of if
            ...} ...}

这是 Logcat 输出:

    11-20 19:00:27.363: E/AndroidRuntime(18311): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.Snow/com.app.snow.Snow}: java.lang.NullPointerException
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at android.os.Handler.dispatchMessage(Handler.java:99)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at android.os.Looper.loop(Looper.java:150)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at android.app.ActivityThread.main(ActivityThread.java:4263)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at java.lang.reflect.Method.invokeNative(Native Method)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at java.lang.reflect.Method.invoke(Method.java:507)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at dalvik.system.NativeStart.main(Native Method)
    11-20 19:00:27.363: E/AndroidRuntime(18311): Caused by: java.lang.NullPointerException
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at com.app.snow.Snow.onCreate(Snow.java:54)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
    11-20 19:00:27.363: E/AndroidRuntime(18311):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
4

2 回答 2

1

尝试:

final int[] picItems = {R.drawable.pic_one, R.drawable.pic_two, R.drawable.pic_three};

这样,它一定会被实例化,而且您将突出显示您可能正在更改数组引用的地方,然后您不可能从访问中获得 NPE picItems,如果您仍然获得 NPE,那么它就是另一回事了。

于 2012-11-20T23:52:55.043 回答
1

但是,picItem 数组为空。

你让我上当了一会儿。picItems很好。relative为空,将其初始化为onCreate()

relative = (RelativeLayout) findViewById(R.id.layout); 
                            // Where "layout" is the RelativeLayout's id
于 2012-11-21T00:13:07.900 回答