我在解决这个问题时遇到了麻烦,我希望有人能向我解释这个问题。我有这个用于解析 JSON 的代码,并且我应该使用这个代码来获取值。
public class MainActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
JSONParser jParser = new JSONParser();
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String RANK = "rank";
static String COUNTRY = "country";
static String POPULATION = "population";
static String FLAG = "flag";
static final String TAG_fraction_id = "fraction_id";
static final String TAG_fraction_name = "fraction_name";
static final String TAG_fraction_type = "fraction_type";
static final String TAG_fraction_typetext = "fraction_typetext";
static final String TAG_fraction_description = "fraction_description";
static final String TAG_fraction_extra = "fraction_extra";
static final String TAG_fraction_company_id = "company_id";
static final String TAG_fraction_pictogram = "fraction_pictogram";
static final String TAG_Company = "company_id";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Android JSON Parse Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String url = "http://ws.mitaffald.dk/webservice/getFractions.php?company_id=0";
JSONArray json = jParser.getJSONFromUrl(url);
try {
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
String fraction_id = c.getString(TAG_fraction_id);
String fraction_name = c.getString(TAG_fraction_name);
String fraction_type = c.getString(TAG_fraction_type);
String fraction_typetext = c.getString(TAG_fraction_typetext);
String fraction_description = c.getString(TAG_fraction_description);
String fraction_extra = c.getString(TAG_fraction_extra);
String company_id = c.getString(TAG_fraction_company_id);
String fraction_pictogram = c.getString(TAG_fraction_pictogram);
HashMap<String, String> map = new HashMap<String, String>();
// Hver knode blive lavet til nøgle : værdi
map.put(TAG_fraction_id, fraction_id);
map.put(TAG_fraction_name, fraction_name);
map.put(TAG_fraction_type, fraction_type);
map.put(TAG_fraction_typetext, fraction_typetext);
map.put(TAG_fraction_description, fraction_description);
map.put(TAG_fraction_extra, fraction_extra);
map.put(TAG_fraction_company_id, company_id);
map.put(TAG_fraction_pictogram, fraction_pictogram);
// tilføj til arraylist
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
mProgressDialog.dismiss();
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
}
}
但是当我运行时,我收到了这个错误:
07-12 00:05:03.480: E/AndroidRuntime(30716): FATAL EXCEPTION: AsyncTask #1
07-12 00:05:03.480: E/AndroidRuntime(30716): java.lang.RuntimeException: An error occured while executing doInBackground()
07-12 00:05:03.480: E/AndroidRuntime(30716): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-12 00:05:03.480: E/AndroidRuntime(30716): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-12 00:05:03.480: E/AndroidRuntime(30716): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-12 00:05:03.480: E/AndroidRuntime(30716): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-12 00:05:03.480: E/AndroidRuntime(30716): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-12 00:05:03.480: E/AndroidRuntime(30716): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-12 00:05:03.480: E/AndroidRuntime(30716): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-12 00:05:03.480: E/AndroidRuntime(30716): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-12 00:05:03.480: E/AndroidRuntime(30716): at java.lang.Thread.run(Thread.java:856)
07-12 00:05:03.480: E/AndroidRuntime(30716): Caused by: java.lang.NullPointerException
07-12 00:05:03.480: E/AndroidRuntime(30716): at com.androidbegin.jsonparsetutorial.MainActivity$DownloadJSON.doInBackground(MainActivity.java:100)
07-12 00:05:03.480: E/AndroidRuntime(30716): at com.androidbegin.jsonparsetutorial.MainActivity$DownloadJSON.doInBackground(MainActivity.java:1)
07-12 00:05:03.480: E/AndroidRuntime(30716): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-12 00:05:03.480: E/AndroidRuntime(30716): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-12 00:05:03.480: E/AndroidRuntime(30716): ... 5 more
07-12 00:05:03.495: D/dalvikvm(30716): GC_CONCURRENT freed 370K, 14% free 9890K/11463K, paused 11ms+2ms, total 35ms
07-12 00:05:07.800: I/Choreographer(30716): Skipped 30 frames! The application may be doing too much work on its main thread.
07-12 00:05:12.425: E/WindowManager(30716): Activity com.androidbegin.jsonparsetutorial.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42124d18 that was originally added here
07-12 00:05:12.425: E/WindowManager(30716): android.view.WindowLeaked: Activity com.androidbegin.jsonparsetutorial.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42124d18 that was originally added here
07-12 00:05:12.425: E/WindowManager(30716): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:403)
07-12 00:05:12.425: E/WindowManager(30716): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
07-12 00:05:12.425: E/WindowManager(30716): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-12 00:05:12.425: E/WindowManager(30716): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-12 00:05:12.425: E/WindowManager(30716): at android.view.Window$LocalWindowManager.addView(Window.java:554)
07-12 00:05:12.425: E/WindowManager(30716): at android.app.Dialog.show(Dialog.java:277)
07-12 00:05:12.425: E/WindowManager(30716): at com.androidbegin.jsonparsetutorial.MainActivity$DownloadJSON.onPreExecute(MainActivity.java:66)
07-12 00:05:12.425: E/WindowManager(30716): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
07-12 00:05:12.425: E/WindowManager(30716): at android.os.AsyncTask.execute(AsyncTask.java:534)
07-12 00:05:12.425: E/WindowManager(30716): at com.androidbegin.jsonparsetutorial.MainActivity.onCreate(MainActivity.java:48)
07-12 00:05:12.425: E/WindowManager(30716): at android.app.Activity.performCreate(Activity.java:5206)
07-12 00:05:12.425: E/WindowManager(30716): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-12 00:05:12.425: E/WindowManager(30716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
07-12 00:05:12.425: E/WindowManager(30716): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-12 00:05:12.425: E/WindowManager(30716): at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-12 00:05:12.425: E/WindowManager(30716): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-12 00:05:12.425: E/WindowManager(30716): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 00:05:12.425: E/WindowManager(30716): at android.os.Looper.loop(Looper.java:137)
07-12 00:05:12.425: E/WindowManager(30716): at android.app.ActivityThread.main(ActivityThread.java:4921)
07-12 00:05:12.425: E/WindowManager(30716): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 00:05:12.425: E/WindowManager(30716): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 00:05:12.425: E/WindowManager(30716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-12 00:05:12.425: E/WindowManager(30716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-12 00:05:12.425: E/WindowManager(30716): at dalvik.system.NativeStart.main(Native Method)
为什么会这样?