2013-07-11 59 views
0

我無法解決這個問題,我希望有人能向我解釋這個問題。我有這個代碼解析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 [email protected] 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 [email protected] 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) 

這究竟是爲什麼?

+0

線100是什麼?還將此行移至onCreate()方法listview =(ListView)findViewById(R.id.listview);以上新的DownloadJSON()。execute(); –

+0

第100行是 - > arraylist.add(map); – Tirolel

+0

作爲你的logcat表明你有NullpointerException在那一行 在com.androidbegin.jsonparsetutorial.MainActivity $ DownloadJSON.doInBackground(MainActivity.java:100),我建議再看看那個 –

回答

1

arraylist已定義但未初始化。你可能想在onCreate中初始化它。

+0

是啊我想出來之前,你鍵入答案。謝謝。 – Tirolel