Java org.json.JSONException:数据没有值

Java org.json.JSONException:数据没有值,java,android,Java,Android,我试图制作一个android应用程序,但我得到了title的例外。我的第一个目标是在TextView中显示json中的一个字符。之后,我想在点击按钮后转到下一个字符。代码是用于学校项目的。我花了几个小时试图找出哪里出了问题,但我找不到错误。我已经阅读了很多stackoverflow问题及其答案,但每段代码的答案都很复杂 package com.example.pirama3.pirama3; import android.content.res.Resources; import androi

我试图制作一个android应用程序,但我得到了title的例外。我的第一个目标是在TextView中显示json中的一个字符。之后,我想在点击按钮后转到下一个字符。代码是用于学校项目的。我花了几个小时试图找出哪里出了问题,但我找不到错误。我已经阅读了很多stackoverflow问题及其答案,但每段代码的答案都很复杂

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]);

    }

}
这是我的activity_main.xml

<?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>

因为这是一个家庭作业,所以我不会在这里做所有的编码(你的作业应该有一些好处)。 通常,这里有一些代码冗余——即多次解析资源文件(每次单击按钮)。因此,将代码打包到它自己的方法中,并只调用它一次——最好从
onCreate

即使您知道数组中将有3999个元素,最好不要使用
publicstring[]ls=newstring[3998]
(为什么
public
无论如何???)一个
ArrayList
将根据需要展开。因此,请这样做:

private ArrayList<String> list;

private void parseJson(String s) {
    list = new ArrayList<>();
    try{
        JSONObject root = new JSONObject(s);
        JSONArray dt = root.getJSONArray("data");
        int len = dt.length();
        for(int i=0; i<len; i++){
            JSONObject chara = dt.getJSONObject(i);
            String s = chara.optString("character", "");
            if(!s.isEmpty()){
                list.add(s);
            }
    }
    }catch (JSONException e){
        e.printStackTrace();
    }
}
现在,每次单击按钮时,只需增加currentIndex的值,并在
TextView中显示
ArrayList

public void loadGrades(View view){
    currentIndex++;
    String s = list.get(currentIndex);
    txt.setText(s);
}
免责声明:我是在一个文本编辑器中这样做的,因此可能会有一些方法拼写错误或语法错误。但是你应该明白


附加说明:您可能希望在后台线程中加载资源文件,并异步加载数据

你的列表中真的有3998个元素吗?或者你只是想在数组中保留足够的空间?请在这里添加json响应。我的列表中确实有3999个元素
private int currentIndex = 0;
public void loadGrades(View view){
    currentIndex++;
    String s = list.get(currentIndex);
    txt.setText(s);
}