2017-04-11 2 views
0

Ich habe folgenden Code in meiner Hauptaktivität.Zugreifen auf String-Array-Ressourcen in einer Aktivität

private List<Notes> chap = new ArrayList<>(); 
Resources res = getResources(); 
String[] chap1Titles = res.getStringArray(R.array.Introductiontitles); 
String[] chap1Notes = res.getStringArray(R.array.Introductionnotes); 
private void initializeData(){ 
chap.add(new Notes(chap1Titles[0],chap1Notes[0])); 
chap.add(new Notes(chap1Titles[1],chap1Notes[1])); 
} 

In Android Studio es für diese code.But keine Fehler geben, wenn ich Aktivität starten nichts, es ist Anzeige und gestoppt wird. Als ich diesen Code wie folgt änderte, funktionierte es. Ich verstehe das Problem mit dem obigen Code nicht.

private List<Notes> chap = new ArrayList<>(); 
private void initializeData(){ 
chap.add(new Notes("Introduction ","About Introduction topic")); 
chap.add(new Notes("Motion ","About Motion topic")); } 

Logcat:

04-11 18: 16: 30.150 24.035 bis 24.035 /? D/dalvikvm: späte Freigabe CheckJNI 04-11 18: 16: 30.290 24035-24035 /? D/ActivityThread: handleBindApplication: com.ilearn.itemdetail 04-11 18: 16: 30.300 24035-24035 /? D/ActivityThread: setTargetHeapUtilization: 0.75 04-11 18: 16: 30.300 24035-24035/com.ilearn.itemDetail D/AktivitätThread: setTargetHeapMinFree: 2097152 04-11 18: 16: 30.370 24035-24035/com.ilearn.itemdetail D/AndroidRuntime: VM herunterfahren 04-11 18: 16: 30.370 24035-24035/com.ilearn.itemdetail Mit dalvikvm: threadid = 1: Thread mit nicht abgefangener Ausnahme beenden (group = 0x415e3d58) 04-11 18:16 : 30.380 24035-24035/com.ilearn.itemdetail E/AndroidRuntime: FATALE AUSNAHME: Haupt Prozess: com.ilearn.itemdetail, PID: 24035 java.lang.RuntimeException: kann Aktivität nicht instanziieren ComponentInfo {com.ilearn.itemdetail/com.ilearn.itemdetail.MainActivity}: java.lang.NullPointerException bei android.app.ActivityThread.performLaunchActivity (Ac tivityThread.java:2126) bei android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2249) bei android.app.ActivityThread.access $ 800 (ActivityThread.java:141) bei android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1212) bei android.os.Handler.dispatchMessage (Handler.java:102) bei android.os.Looper.loop (Looper.java:136) bei android.app.ActivityThread.main (ActivityThread .java: 5113) bei java.lang.reflect.Method.invokeNative (Native Methode) bei java.lang.reflect.Method.invoke (Method.java:515) bei com.android.internal.os.ZygoteInit $ Metho dAndArgsCaller.run (ZygoteInit.java:793) bei com.android.internal.os.ZygoteInit.main (ZygoteInit.java:609) bei dalvik.system.NativeStart.main (native Methode) verursacht durch: java.lang .NullPointerException bei android.content.ContextWrapper.getResources (ContextWrapper.java:89) bei android.view.ContextThemeWrapper.getResources (ContextThemeWrapper.java:78) bei com.ilearn.itemdetail.MainActivity. (MainActivity.java:23) bei java.lang.Class.newInstanceImpl (native Methode) bei java.lang.Class.newInstance (Class.java:1208) bei android.app.Instrumentation.newActivity (Inst rumentation.java:1061) bei android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2117) bei android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2249) bei android.app.ActivityThread.access $ 800 (ActivityThread .java: 141) bei android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1212) bei android.os.Handler.dispatchMessage (Handler.java:102) bei android.os.Looper.loop (Looper .java: 136) bei android.app.ActivityThread.main (ActivityThread.java:5113) bei java.lang.reflect.Method.invokeNative (Native Methode) bei java.lang.reflect.Method.invoke (Method.java : 515) bei com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793) bei com.android.internal.os.ZygoteInit.main (ZygoteInit.java:609) bei dalvik.system .NativeStart.main (native Methode)

+1

Sie Ihre logcat veröffentlichen können hier – Lokesh

+0

Sorry, aber ich es jetzt nicht – Kokatesk

+0

drücken Sie Alt + 6 in Android Studio haben Sie die logcats bekommen – Lokesh

Antwort

0

diesen Code Versuchen:

private List<Notes> chap = new ArrayList<>(); 
Resources res = getResources(); 
String[] chap1Titles; 
String[] chap1Notes; 
private void initializeData() { 
    chap1Titles = res.getStringArray(R.array.Introductiontitles); 
    chap1Notes = res.getStringArray(R.array.Introductionnotes); 
    chap.add(new Notes(chap1Titles[0],chap1Notes[0])); 
    chap.add(new Notes(chap1Titles[1],chap1Notes[1])); 
} 
0

Ich denke, Sie sind etwas falsch initialisiert. Bitte schreiben Sie Code auf diese Weise und versuchen Sie es erneut:

private List<Notes> chap = new ArrayList<>(); 
String[] chap1Titles; 
String[] chap1Notes; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    initializeData(); 
} 

private void initializeData() { 
    chap1Titles = getResources().getStringArray(R.array.Introductiontitles); 
    chap1Notes = getResources().getStringArray(R.array.Introductionnotes); 
    chap.add(new Notes(chap1Titles[0], chap1Notes[0])); 
    chap.add(new Notes(chap1Titles[1], chap1Notes[1])); 
} 
Verwandte Themen