2017-08-03 29 views
0

我想從使用Android Volley庫的遠程服務器上的數據庫中獲取數組。問題是JSONobject未在onResponse方法中返回任何數據。當我嘗試在JSONobject中顯示數據時,應用程序崩潰並且logcat顯示NullPointerException。下面JsonArrayRequest沒有給出任何迴應

Java代碼給出:

public class AudioFragmentThree extends Fragment { 
    View view; 
    TextView text; 
    String url = "http://www.muftiattaullahmultani.com/android/get_all_bayan.php"; 
    int count = 0; 
    int i = 0; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.audio_fragment_three, container, false); 
     return view; 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, url, null, new Response.Listener<JSONArray>() { 
      public void onResponse(JSONArray response) { 
       while(count<response.length()) 
       { 
        JSONObject object= null; 
        try { 
         object = response.getJSONObject(count); 
         String s = object.getString("id") +" "+ object.getString("topic"); 

         text.setText(s); 

         count++; 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       text.setText("ERROR"); 
      } 
     }); 

     MySingleton.getInstance(getActivity()).addToRequestQueue(jsonArrayRequest); 
    } 
} 

logcat的是如下:

FATAL EXCEPTION: main Process: com.example.mashood.muftiattaullahmultanicom, PID: 22596 
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at 
com.example.mashood.muftiattaullahmultanicom.AudioFragmentThree$1.onResponse(AudioFragmentThree.java:81) 
     at com.example.mashood.muftiattaullahmultanicom.AudioFragmentThree$1.onResponse(AudioFragmentThree.java:69) 
     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65) 
     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5910) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

下面是我的PHP代碼:

<?PHP include 'database.php'; 
$query = 'SELECT * FROM audio_bayan ORDER BY no DESC'; 
$result = $conn->query($query); 
$response = array(); 
while($row = $result->fetch_assoc()) { 
    array_push($response,array("id"=>$row['no'],"topic"=>$row['topic'])); 
} 
echo json_encode($response); 
?> 

回答

2

你必須定義textview象下面這樣:

Textview text; 

text = (TextView) view.findViewById(R.id.textid); 
1

看起來你的TextView爲空。

顯示java.lang.NullPointerException:嘗試在 空對象引用調用虛擬方法 '空隙android.widget.TextView.setText(java.lang.CharSequence中)'

這意味着你正嘗試在空引用上調用setText。你必須在onCreateViewonActivityCreated分配您的TextView從xml配置TextView的,像這樣的一個參考:

TextView text =(TextView) view.findViewById(R.id.text_view); 

其中,text_view是XML你的TextView的id。

1

你忘了你的findViewById領域text附加:

text = (TextView) view.findViewById(R.id.text_id); 

onCreateView方法

相關問題