-1

I know I have seen same question on stackoverflow and tried a lot to solve it but I failed , I am new to android studio simply want to read json file but it always throw me this error , please do not give me negative reputation or put in duplicate question. I am beginner and this is my 5th day I am spending on it Function is very simple , it will read json file from the server and print it in mainactivity , one activity will be fetch activity will will do job in background

This is my xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/parse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="683dp"
        android:text="parse" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/parse">

        <TextView
            android:id="@+id/prasedata"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="fetch text here" />
    </ScrollView>


</RelativeLayout>

This is main activity

package com.codewithosama.volleydemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.jar.JarEntry;

public class MainActivity extends AppCompatActivity {
    Button click ;
    public  static  TextView data ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        click = (Button) findViewById(R.id.parse);
        data = (TextView)findViewById(R.id.prasedata);
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fetchData process =new fetchData();
                process.execute();

            }
        });
    }
}

This is fetchDAta activity

package com.codewithosama.volleydemo;

import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class fetchData extends AsyncTask<Void, Void, String> {
    String data="";

    @Override
    protected String doInBackground(Void... voids) {
        try {
            URL url = new URL("https://jsonplaceholder.typicode.com/todos/1");
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
            InputStream inputStream = httpsURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line="";
            while (line != null){
                line=bufferedReader.readLine();
                data= data + line;
            }


        }catch (MalformedURLException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String aVoid) {
        super.onPostExecute(aVoid);
        MainActivity.data.setText(this.data);
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Osama
  • 94
  • 5

1 Answers1

1

You forgot to call setContentView() in your MainActivity. So it should be

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main) // your activity's layout
    // rest of your code
}
Artem
  • 51
  • 4
  • 2020-04-14 14:09:57.829 2658-2658/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2020-04-14 14:09:57.830 2658-2658/? E/android.os.Debug: failed to load memtrack module: -2 – Osama Apr 14 '20 at 09:11
  • These error is showing and now there is not button or text view is showing on it – Osama Apr 14 '20 at 09:11
  • @Osama did you change `R.layout.activity_main` to your layout's id? – Artem Apr 14 '20 at 09:55