0

Here my json file: settings.json (in assets folder):

{
  "settings": [
    {
      "level": "upper"
    }
  ]
}

I working in Android Studio. I print the upper word in a TextView from the json file, but this not working, my app is crashed. Ideas? my code in java file:

public class MainActivity extends AppCompatActivity {
    ArrayList<String> level = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {

            JSONObject obj = new JSONObject(loadJSONFromAsset());

            JSONArray userArray = obj.getJSONArray("settings");

            for (int i = 0; i < userArray.length(); i++) {

                JSONObject userDetail = userArray.getJSONObject(i);

                level.add(userDetail.getString("level"));
            }
            } catch(JSONException e){
                e.printStackTrace();

            }
        TextView textView = (TextView) findViewById(R.id.textView);

        textView.setText(level.get(0));

        setContentView(R.layout.activity_main);
    }
    public String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream is = getAssets().open("settings.json");

            int size = is.available();

            byte[] buffer = new byte[size];

            is.read(buffer);

            is.close();

            json = new String(buffer, "UTF-8");


        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

Thank you. Android, json, source code...

2 Answers 2

1

And why the else working in the if function? The level's record is upper. Why not true the if?

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        try {
            // get JSONObject from JSON file
            JSONObject obj = new JSONObject(loadJSONFromAsset());
            // fetch JSONArray named users
            JSONArray userArray = obj.getJSONArray("settings");
            // implement for loop for getting users list data
            for (int i = 0; i < userArray.length(); i++) {
                // create a JSONObject for fetching single user data
                JSONObject userDetail = userArray.getJSONObject(i);
                // fetch email and name and store it in arraylist
                level.add(userDetail.getString("level"));
            }
            } catch(JSONException e){
                e.printStackTrace();

            }
        TextView textView = (TextView) findViewById(R.id.textView);

        if (level.get(0) == "upper") {
            textView.setText(level.get(0));
        } else {

        }
Sign up to request clarification or add additional context in comments.

Comments

0

I hope this will work for you,

Write Your onCreate() method like this,

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

            JSONObject obj = new JSONObject(loadJSONFromAsset());
            JSONArray userArray = obj.getJSONArray("settings");
            for (int i = 0; i < userArray.length(); i++) {
                JSONObject userDetail = userArray.getJSONObject(i);
                level.add(userDetail.getString("level"));
            }
            } catch(JSONException e){
                e.printStackTrace();

            }
        TextView textView = (TextView) findViewById(R.id.textView);

        textView.setText(level.get(0));
    }

set setContentView() method first then initialize your textview.In your code you accessing text view before layout initialized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.