单击方法

时间:2017-09-06 22:29:45

标签: php android jquery json

我正在尝试创建我的应用程序的一部分,我可以为我的应用程序获取JSON,我最终将其添加到ListView。然而,对于时间问题,我只想在单击Json按钮时显示JSON php文件。

下面是我点击我的getJSON按钮

时得到的错误

更新的错误日志和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)

以下是我的XML代码

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

以下是我班级的代码:

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;
        }
    }



}

1 个答案:

答案 0 :(得分:1)

方法getJSON应该接收一个View参数

getJSON(View v)

如果你密切关注错误,你会发现你很清楚你错过了什么Could not find method getJSON(View) in a parent or ancestor