I get this crash error report when i'm push the application button to make array request:
当我按下应用程序按钮发出数组请求时,我收到此崩溃错误报告:
01-31 16:55:51.713 3745-3745/com.example.guidofabbrini.easyracevolley E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.guidofabbrini.easyracevolley, PID: 3745
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.guidofabbrini.easyracevolley.AppController.addToRequestQueue(com.android.volley.Request)' on a null object reference
at com.example.guidofabbrini.easyracevolley.MainActivity.makeJsonArrayRequest(MainActivity.java:120)
at com.example.guidofabbrini.easyracevolley.MainActivity.access$000(MainActivity.java:21)
at com.example.guidofabbrini.easyracevolley.MainActivity$1.onClick(MainActivity.java:54)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Here's my code
这是我的代码
public class MainActivity extends Activity {
// json array response url
private String urlJsonArry ="http://pastebin.com/raw/puYnpK96";
private static String TAG = MainActivity.class.getSimpleName();
private Button btnMakeArrayRequest;
// Progress dialog
private ProgressDialog pDialog;
private TextView txtResponse;
// temporary string to show the parsed response
private String jsonResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
txtResponse = (TextView) findViewById(R.id.txtResponse);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// making json array request
makeJsonArrayRequest();
}
});
}
/**
* Method to make json array request where response starts with [
* */
private void makeJsonArrayRequest() {
showpDialog();
JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
// Parsing json array response
// loop through each json object
jsonResponse = "";
for (int i = 0; i < response.length(); i++) {
JSONObject data = (JSONObject) response
.get(i);
String position = data.getString("position");
JSONObject pilot = data
.getJSONObject("pilot");
String name = pilot.getString("name");
String lapcount = data.getString("lap_count");
String avglaptime = data.getString("avg_lap_time");
jsonResponse += "Position: " + position + "\n";
jsonResponse += "Pilot: " + name + "\n";
jsonResponse += "LapCount: " + lapcount + "\n";
jsonResponse += "Avg_LapTime: " + avglaptime + "\n\n";
}
txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hidepDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
My Application
我的应用程序
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
1 个解决方案
#1
0
It seems that you are missing a single line in your AndroidManifest.xml under the application tag:
您似乎在应用程序标记下的AndroidManifest.xml中缺少一行:
<application
android:name="YourPackage.AppController"
this line is indicating the fully qualified name of the Application subclass that you implemented.
此行指示您实现的Application子类的完全限定名称。
#1
0
It seems that you are missing a single line in your AndroidManifest.xml under the application tag:
您似乎在应用程序标记下的AndroidManifest.xml中缺少一行:
<application
android:name="YourPackage.AppController"
this line is indicating the fully qualified name of the Application subclass that you implemented.
此行指示您实现的Application子类的完全限定名称。