JsonArrayRequest没有给出任何回复

时间:2017-08-03 13:11:10

标签: java php android android-volley

我正在尝试使用Android Volley库从远程服务器上的数据库中获取数组。问题是JSONobject没有返回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); } } 方法中的任何数据。当我尝试在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) 中显示数据时,应用程序崩溃并且logcat显示NullPointerException。

Java代码如下:

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

LogCat如下:

php.ini

以下是我的PHP代码:

extension=php_pdo_sqlite.dll

extension=php_sqlite3.dll

3 个答案:

答案 0 :(得分:2)

您必须定义textview,如下所示:

Textview text;

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

答案 1 :(得分:1)

看起来你的TextView为空。

  

java.lang.NullPointerException:尝试调用虚方法        'void android.widget.TextView.setText(java.lang.CharSequence)'上          null对象引用

这意味着您尝试在空引用上调用setText。您必须将TextView的参考从xml布局分配给TextView,如onCreateViewonActivityCreated中所示:

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

其中,text_view是xml中TextView的id。

答案 2 :(得分:1)

您忘记了findViewById您的text字段添加:

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

到您的onCreateView方法