Ich versuche, eine Android-Anwendung zu machen, aber ich bekomme die Ausnahme des Titels. Mein erstes Ziel ist es, in TextView einen Charakter von json anzuzeigen. Danach möchte ich nach Klick auf den Button zum nächsten Zeichen gehen. Der Code ist für ein Schulprojekt. Ich versuche stundenlang zu finden, was schief geht, aber ich kann den Fehler nicht finden. Ich habe viele stackoverflow Fragen und ihre Antworten gelesen, aber die Antworten sind für jedes Stück Code ausgefeilt.org.json.JSONException: Kein Wert für Daten
package com.example.pirama3.pirama3;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MainActivity extends AppCompatActivity {
public List<String> list =new ArrayList<String>();
public String[] ls= new String[3998];
public void loadGrades(View view){
Resources res = getResources();
InputStream is = res.openRawResource(R.raw.test02);
Scanner scanner = new Scanner(is);
StringBuilder builder = new StringBuilder();
while(scanner.hasNextLine()){
builder.append(scanner.nextLine());
}
parseJson(builder.toString());
}
private void parseJson(String s) {
TextView txt = (TextView) findViewById(R.id.abc);
StringBuilder builder = new StringBuilder();
try{
JSONObject root = new JSONObject(s);
JSONArray dt = root.getJSONArray("data");
for(int i=0;i<3999;i++){
JSONObject chara = dt.getJSONObject(i);
Log.d("aaa",chara.getString("character"));
ls[i]=chara.optString("character");
}
}catch (JSONException e){
e.printStackTrace();
}
//Log.d("abcc",list.get(0));
//txt.setText(s);
txt.setText(ls[1]);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt = (TextView) findViewById(R.id.abc);
//Log.d("abcc",list.get(0));
//System.out.println(list.get(0));Resources res = getResources();
Resources res = getResources();
InputStream is = res.openRawResource(R.raw.test02);
Scanner scanner = new Scanner(is);
StringBuilder builder = new StringBuilder();
while(scanner.hasNextLine()){
builder.append(scanner.nextLine());
}
try {
JSONObject root = new JSONObject(builder.toString());
JSONArray dt = root.getJSONArray("data");
//ls = new String[dt.length()];
}catch (JSONException ab){
ab.printStackTrace();
}
txt.setText(ls[1]);
}
}
Meine JSON-Datei ist wie unten
{
data:[
{
character:"a",
pronunciation:"a"
},
{
character:"b",
"pronunciation:"b"
}
]
}
Das ist mein activity_main.xml ist
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pirama3.pirama3.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadGrades"
android:text="@string/button_load_grades"/>
<TextView
android:id="@+id/abc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20dp"
android:text="@string/text_student_info" />
</LinearLayout>
Haben Sie wirklich 3998 Elemente in Ihrer Liste? Oder versuchen Sie nur, genügend Speicherplatz in Ihrem Array zu reservieren? – Barns
Bitte fügen Sie Ihre JSON-Antwort hier hinzu. –
Ich habe wirklich 3999 Elemente in meiner Liste –