2017-09-06 60 views
-2

我想創建我的應用程序的一部分,我可以爲我的應用程序獲取JSON,最終將其添加到ListView中。但是對於tim,我只是希望JSON php文件在我點擊Json按鈕時出現。我的getJSON上Click方法的錯誤

下面是我得到當我點擊我的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

方法的getJSON如果你仔細觀察到的錯誤,你會看到,這是很清楚你缺少Could not find method getJSON(View) in a parent or ancestor

+0

仍然沒有工作應該接受一個View參數

getJSON(View v) 

。我添加了getJSON(View v)。代碼更新 – user8534232

+0

你得到的錯誤是什麼? – drac94

+0

java.lang.IllegalStateException:找不到方法getJSON(視圖)在父或祖先上下文的android:onClick屬性定義在視圖類android.support.v7.widget.AppCompatButton id爲'buttonjson' – user8534232