douzhang3822 2017-08-03 13:11
浏览 58
已采纳

JsonArrayRequest没有给出任何回复

I am trying to fetch an array from a database on a remote server using the Android Volley library. The problem is that the JSONobject is not returning any data in the onResponse method. When I try to show the data in JSONobject, the application crashes and logcat shows a NullPointerException.

Java code is given below:

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 is given below:

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)

Below is my PHP code:

<?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);
?>
  • 写回答

3条回答 默认 最新

  • dongshangan2074 2017-08-03 13:14
    关注

    You have to define textview like below:

    Textview text;
    
    text = (TextView) view.findViewById(R.id.textid);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?