-2

I am trying to create a section of my application where I can fetch the JSON for my app, where I will eventually add it to a ListView. However for the tim being I just want the JSON php file to appear when I click the Json button.

Below is the Error which I am getting when i Click my getJSON button,

UPDATED ERROR LOG AND JAVA CLASS

 09-07 12:36:00.403 29439-29439/com.example.aids.a09application E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.aids.a09application, PID: 29439
                                                                                 java.lang.IllegalStateException: Could not find method getJSON(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'buttonjson'
                                                                                     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
                                                                                     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                                     at android.view.View.performClick(View.java:6213)
                                                                                     at android.widget.TextView.performClick(TextView.java:11074)
                                                                                     at android.view.View$PerformClick.run(View.java:23645)
                                                                                     at android.os.Handler.handleCallback(Handler.java:751)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

Below is my XML Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Get JSON"
android:layout_gravity="center_horizontal"
android:onClick="getJSON"
android:id="@+id/buttonjson"/>

<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Parse JSON"
android:layout_gravity="center_horizontal"
android:id="@+id/buttonparsejson"
android:layout_marginTop="20dp"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="JSON Appears Below"
android:textAppearance="?android:textAppearanceLarge"
android:gravity="center"
android:layout_marginTop="20dp"
android:id="@+id/jsonTextView"/>


</LinearLayout>

Below is the Code for my class:

public class StandingsList extends Fragment {
    String JSON_STRING;
    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate( R.layout.fragment_standings, container, false );
        textView = (TextView) view.findViewById( R.id.jsonTextView );
        return view;

    }


    public void getJSON(View v) {

        new BackgroundTask().execute();
    }

    class BackgroundTask extends AsyncTask<String, Void, String> {

        String json_url;

        @Override
        protected void onPreExecute() {
            json_url = "http://163.172.142.145/get_info.php";
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                URL url = new URL( json_url );
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( inputStream ) );
                StringBuilder stringBuilder = new StringBuilder();

                while ((JSON_STRING = bufferedReader.readLine()) != null) {

                    stringBuilder.append( JSON_STRING + "\n" );

                }

                inputStream.close();
                bufferedReader.close();
                httpURLConnection.disconnect();

                return stringBuilder.toString().trim();

            } catch (MalformedURLException e) {
                Log.e( TAG, "MalformedURLException: " + e ); //print exception message to log
            } catch (IOException e) {
                Log.e( TAG, "IOException: " + e ); //print exception message to log
            }
            return null;

        }

        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate( values );
        }

        @Override
        protected void onPostExecute(String result) {
            textView.setText( result );
            JSON_STRING = result;
        }
    }



}
user8534232
  • 57
  • 2
  • 8

1 Answers1

1

The method getJSON should receive a View parameter

getJSON(View v)

if you look closely into the error you will see that it is very clear what you are missing Could not find method getJSON(View) in a parent or ancestor

ludwiguer
  • 2,177
  • 2
  • 15
  • 21
  • Still did not work. I added getJSON(View v). Code Updated – user8534232 Sep 07 '17 at 11:38
  • java.lang.IllegalStateException: Could not find method getJSON(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'buttonjson' – user8534232 Sep 08 '17 at 20:03
  • I see, although your button is in the fragment it will try to call the method in the activity, a better approach would be to implement the onClickListener to the fragment, take a look to this code https://stackoverflow.com/a/14571018/1128070 – ludwiguer Sep 08 '17 at 23:48