我有一个Web管理面板,它有一个api,这个api生成Json数据。所以现在我制作了一个Android应用程序来检索这个json数据并在应用程序屏幕上显示它。我现在得到的问题是每当我运行这个应用程序,它正确加载,然后我点击“获取数据”按钮后,asyncTask运行,然后应用程序崩溃几秒钟后。通过日志猫我无法理解这个问题的原因。所以请有人帮我解决这个问题。贝娄是我得到的日志猫视图:
05-31 05:36:33.179: D/AndroidRuntime(973): Shutting down VM
05-31 05:36:33.179: W/dalvikvm(973): threadid=1: thread exiting with uncaught exception (group=0xb4a2fba8)
05-31 05:36:33.289: E/AndroidRuntime(973): FATAL EXCEPTION: main
05-31 05:36:33.289: E/AndroidRuntime(973): Process: com.example.json, PID: 973
05-31 05:36:33.289: E/AndroidRuntime(973): java.lang.NullPointerException
05-31 05:36:33.289: E/AndroidRuntime(973): at com.example.json.MainActivity$JSONParse.onPostExecute(MainActivity.java:82)
05-31 05:36:33.289: E/AndroidRuntime(973): at com.arifhaq.json.MainActivity$JSONParse.onPostExecute(MainActivity.java:1)
05-31 05:36:33.289: E/AndroidRuntime(973): at android.os.AsyncTask.finish(AsyncTask.java:632)
05-31 05:36:33.289: E/AndroidRuntime(973): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-31 05:36:33.289: E/AndroidRuntime(973): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
05-31 05:36:33.289: E/AndroidRuntime(973): at android.os.Handler.dispatchMessage(Handler.java:102)
05-31 05:36:33.289: E/AndroidRuntime(973): at android.os.Looper.loop(Looper.java:136)
05-31 05:36:33.289: E/AndroidRuntime(973): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-31 05:36:33.289: E/AndroidRuntime(973): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 05:36:33.289: E/AndroidRuntime(973): at java.lang.reflect.Method.invoke(Method.java:515)
05-31 05:36:33.289: E/AndroidRuntime(973): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-31 05:36:33.289: E/AndroidRuntime(973): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-31 05:36:33.289: E/AndroidRuntime(973): at dalvik.system.NativeStart.main(Native Method)
以下是我的应用程序的主要活动方法:
public class MainActivity extends Activity {
Button Btngetdata;
// URL to get JSON Array
private static String url = "http://10.0.2.2/bootstrap-dist/adminPanel/";
// JSON Node Names
private static final String TAG_USER = "record";
private static final String TAG_NAME = "Name";
private static final String TAG_PRICE = "Price";
private static final String TAG_DESCRIPTION = "Description";
JSONArray user = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btngetdata = (Button) findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
JSONObject c=null;
try {
// Getting JSON Array
user = json.getJSONArray(TAG_USER);
for(int i=0;i<user.length();i++){
c = user.getJSONObject(i);
// Storing JSON item in a Variable
String name = c.getString(TAG_NAME);
String price = c.getString(TAG_PRICE);
String description = c.getString(TAG_DESCRIPTION);
Toast.makeText(MainActivity.this, name+"\n"+price+"\n"+description, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}