2016-11-19 3 views
0

このエラーを修正する方法が見つからない場合は、 原因:java.lang.NullPointerException:仮想を呼び出そうとしていますnullオブジェクト参照のメソッド 'android.content.res.Resourcesのandroid.content.Context.getResourcesは()'javaクラスを使用してリソースを取得できません.error:仮想メソッドを呼び出そうとしました

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

/** * 18/11/2016にInezaによって作成されます。ここ */

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

の一部です。 static初期化子は、クラスのインスタンスが作成される前に実行されます。 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()' –

関連する問題