2 zhengdanimu zhengdanimu 于 2012.11.21 11:18 提问

数组的Drawable路径返回空值

我创建了一个整型值的数组,这些整型值是一个图片对象的路径ID,就像这样

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

然后,picItem的数组是空的。我想要能够从一个索引里取到这些值然后用它作为背景,就像这样:

relative.setBackgroundResource(picItems[index]);

现在,如果我用上边的代码然后设置参数字段为R.drawable.pic_one,这是可以的。但是,当我进行申明然后实例化整形数组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 Output:

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)

3个回答

Sueyexin
Sueyexin   2012.11.21 15:15
已采纳

我怎么感觉我被愚弄了。picItems是正常的,相对的是空,在onCreate()初始化它

relative = (RelativeLayout) findViewById(R.id.layout); 
Sueyexin
Sueyexin 非常高兴我能帮到你, 这个可能不对: if (!(picItems == null) || !(picItems.equals(null))){, 你可以只用 if(picItems != null){.
接近 5 年之前 回复
zhengdanimu
zhengdanimu 额,我居然把这个给忘了,仔细检查了也还是忘了。非常感谢!
接近 5 年之前 回复
niangzhi
niangzhi   2012.11.21 15:31

试试这个:

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

这样,肯定是要实例化的,而且你可能会改变数组的引用,然后你就不可能通过访问picItems获得NPE,如果你们仍然确实要获得NPE,然后还有一些其他的。

qq_27578023
qq_27578023   2016.06.03 20:50

relative = (RelativeLayout) findViewById(R.id.layout);

Csdn user default icon
上传中...
上传图片
插入图片