2016-11-19 31 views
0

任何人都可以幫助我我找不到如何修復此錯誤 引起:java.lang.NullPointerException:嘗試調用虛擬方法 'android.content.res.Resources android.content.Context.getResources()' 上的空對象引用我無法使用java類獲取資源.error:試圖調用虛擬方法

import java.util.ArrayList; 
import java.util.List; 

/** * 創建者Ineza上18/11/2016。 */

public class DerpData extends Application { 
    private static Context mContext; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    mContext = this; 
} 

public static Context getContext(){ 
    return mContext; 
} 



private static final String[] titles = DerpData.getContext().getResources().getStringArray(R.array.listArray); 
private static final int[] icons =DerpData.getContext().getResources().getIntArray(R.array.imgs); 


public static List<LisItem> getListData() { 
    List<LisItem> data = new ArrayList<>(); 

    //Repeat process 4 times, so that we have enough data to demonstrate a scrollable 
    //RecyclerView 
    for (int x = 0; x < 4; x++) { 
     //create ListItem with dummy data, then add them to our List 
     for (int i = 0; i < titles.length && i < icons.length; i++) { 
      LisItem item = new LisItem(); 
      item.setImageResId(icons[i]); 
      item.setTittle(titles[i]); 
      data.add(item); 
     } 
    } 
    return data; 
} 

}

這裏是logcat的

11-19 06:07:42.188 6497-6497/com.example.ineza.assthree E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.ineza.assthree, PID: 6497 
    java.lang.ExceptionInInitializerError 
    at com.example.ineza.assthree.RecyClerViewActivity.onCreate(RecyClerViewActivity.java:27) 
    at android.app.Activity.performCreate(Activity.java:6664) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
    at com.example.ineza.assthree.model.DerpData.<clinit>(DerpData.java:30) 
    at com.example.ineza.assthree.RecyClerViewActivity.onCreate(RecyClerViewActivity.java:27)  
    at android.app.Activity.performCreate(Activity.java:6664)  
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)  
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
    at android.app.ActivityThread.-wrap12(ActivityThread.java)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:154)  
    at android.app.ActivityThread.main(ActivityThread.java:6077)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  
11-19 06:07:42.223 1326-1337/system_process W/ActivityManager: Force finishing activity com.example.ineza.assthree/.RecyClerViewActivity 
11-19 06:07:42.284 1326-1337/system_process W/ActivityManager: Force finishing activity com.example.ineza.assthree/.LVCustomActivity 
11-19 06:07:42.348 1326-1326/system_process W/art: Long monitor contention with owner Binder:1326_1 (1337) at void com.android.server.am.AppErrors.crashApplicationInner(com.android.server.am.Proc essRecord, android.app.ApplicationErrorReport$CrashInfo)(AppErrors.java:328) waiters=0 in int com.android.server.am.ActivityManagerService.broadcastIntent(android.app.IApplicationThread, android.content.Intent, java.lang.String, android.content.IIntentReceiver, int, java.lang.String, android.os.Bundle, java.lang.String[], int, android.os.Bundle, boolean, boolean, int) for 144ms 
11-19 06:07:42.561 1326-1343/system_process E/BatteryStatsService: power: Missing API 
11-19 06:07:42.562 1326-1375/system_process D/WifiNative-HAL: Failing getSupportedFeatureset because HAL isn't started 
11-19 06:07:42.569 1326-1343/system_process E/BluetoothAdapter: Bluetooth binder is null 
11-19 06:07:42.571 1326-1343/system_process E/BatteryStatsService: no controller energy info supplied 
11-19 06:07:42.620 1326-1343/system_process E/BatteryStatsService: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0} 
11-19 06:07:42.723 1326-2409/system_process I/OpenGLRenderer: Initialized EGL, version 1.4 
+1

你可以添加logcat的? – emrekose26

+0

它完成@ emrekose26 – Ineza

回答

1

的代碼試圖初始化類時從靜態上下文訪問靜態變量的一部分。靜態初始化器在類的任何實例創建之前執行。在生命週期回調期間創建實例後,mContext被初始化。更改您這樣的代碼(根據更新下面評論):

@Override 
public void onCreate() { 
    super.onCreate(); 
    mContext = this; 
    titles = mContext.getResources().getStringArray(R.array.listArray); 
    icons = mContext.getResources().getIntArray(R.array.imgs); 
} 

private static final String[] titles; 
private static final int[] icons; 
+1

更好地使用'titles = mContext.getResources()' –

相關問題