程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-數組的Drawable路徑返回空值

java-數組的Drawable路徑返回空值

編輯:編程綜合問答
數組的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)

最佳回答:


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

relative = (RelativeLayout) findViewById(R.id.layout); 
zhengdanimu
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved