不必為每一次HTTP請求都創建一個RequestQueue對象,推薦在application中作初始化
所以
public class MyApplication extends Application { public static RequestQueue requestQueue; @Override public void onCreate() { super.onCreate(); requestQueue = Volley.newRequestQueue(this); } }
之後要呼叫時
RequestQueue mQueue = MyApplication.requestQueue;
StringRequest getStringRequest = new StringRequest("網址", new ResponseListener(),
new ResponseErrorListener()); mQueue.add(getStringRequest);但執行結果出現
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.Request com.android.volley.RequestQueue.add(com.android.volley.Request)' on a null object reference
解決辦法
ref:
http://stackoverflow.com/a/31302226
AndroidManifest.xml中
<application android:name="YOURPACKAGENAME.AppController"
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
沒有留言:
張貼留言