Fatal Error mit Spinner und JSON Object

  • Antworten:3
  • OffenNicht stickyBentwortet

11.02.2013 12:06:29 via Website

Ich möchte die Texte, die im Spinner erscheinen, aus einer JSON-Object auslesen, erhalte aber die Meldung Fatal Error. Testhalber generiere ich das JSON Objekt im gleichen Programmteil, effektiv sollte das JSON-Objekt von einem Server übermittelt werden.

Mein Java lautet:
1protected void onCreate(Bundle savedInstanceState) {
2 super.onCreate(savedInstanceState);
3 setContentView(R.layout.activity_main);
4 array_text=new String[5];
5 JSONObject obj=new JSONObject();
6 try {
7 obj.put("0","");
8 obj.put("1","Türe");
9 obj.put("2","Fenster");
10 obj.put("3","Licht");
11 } catch (JSONException e1) {
12 // TODO Auto-generated catch block
13 }
14 try {
15 array_text[0]=obj.getString("0");
16 array_text[1]=obj.getString("1");
17 array_text[2]=obj.getString("2");
18 array_text[3]=obj.getString("3");
19 } catch (JSONException e) {
20 array_text[0]="";
21 array_text[1]="Tür";
22 array_text[2]="Fenster";
23 array_text[3]="Licht";
24 }
25 Spinner spinner = (Spinner) findViewById(R.id.spinner1);
26 ArrayAdapter adapter= new ArrayAdapter(this, android.R.layout.simple_spinner_item, array_text);
27 spinner.setAdapter(adapter);
28 }

Die Fehlermeldung lautet:
112-14 01:17:06.430: D/AndroidRuntime(6541): Shutting down VM
212-14 01:17:06.430: W/dalvikvm(6541): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
312-14 01:17:06.460: E/AndroidRuntime(6541): FATAL EXCEPTION: main
412-14 01:17:06.460: E/AndroidRuntime(6541): java.lang.NullPointerException
512-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
612-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
712-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:329)
812-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:325)
912-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
1012-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
1112-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.ListView.onMeasure(ListView.java:1109)
1212-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.View.measure(View.java:8171)
1312-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
1412-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
1512-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
1612-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
1712-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.View.measure(View.java:8171)
1812-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
1912-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
2012-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
2112-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
2212-14 01:17:06.460: E/AndroidRuntime(6541): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
2312-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.View.measure(View.java:8171)
2412-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
2512-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
2612-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.View.measure(View.java:8171)
2712-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
2812-14 01:17:06.460: E/AndroidRuntime(6541): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
2912-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.View.measure(View.java:8171)
3012-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
3112-14 01:17:06.460: E/AndroidRuntime(6541): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
3212-14 01:17:06.460: E/AndroidRuntime(6541): at android.os.Handler.dispatchMessage(Handler.java:99)
3312-14 01:17:06.460: E/AndroidRuntime(6541): at android.os.Looper.loop(Looper.java:123)
3412-14 01:17:06.460: E/AndroidRuntime(6541): at android.app.ActivityThread.main(ActivityThread.java:4627)
3512-14 01:17:06.460: E/AndroidRuntime(6541): at java.lang.reflect.Method.invokeNative(Native Method)
3612-14 01:17:06.460: E/AndroidRuntime(6541): at java.lang.reflect.Method.invoke(Method.java:521)
3712-14 01:17:06.460: E/AndroidRuntime(6541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
3812-14 01:17:06.460: E/AndroidRuntime(6541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
3912-14 01:17:06.460: E/AndroidRuntime(6541): at dalvik.system.NativeStart.main(Native Method)
4012-14 01:17:08.881: I/Process(6541): Sending signal. PID: 6541 SIG: 9
Wenn ich das Array direkt definiere, funktioniert das Ganze
1array_text[0]="";
2array_text[1]="Türe";
3array_text[2]="Fenster";
4array_text[3]="Licht";
Leider habe ich noch kaum Erfahrung mit JSON und kann mir das Ganze nicht erklären.
Ich bin dankbar für jeden Hinweis.

11.02.2013 14:04:49 via Website

Du erzeugst ein array für 5 String-Elemente:
1array_text=new String[5];

Befüllst es aber nur mit 4 strings, das Element array_text[4] wird nicht zugewiesen und ist daher --> null

versuchs mal mit:
1array_text=new String[4];

— geändert am 11.02.2013 14:05:16