2012-07-08 80 views
-2

應用程序在幾秒鐘後關閉。我可以在logcat中看到JSON值,但在列表視圖中不會更新結果。解析異步中的JSON時關閉強制任務

我的主要活動類下面的代碼:

public class MainActivity extends ListActivity { 
    private ProgressDialog dialog; 
    private static String url="url here"; 


    private static final String BASE_TAG="base"; 
    private static final String RATES_TAG="rates"; 

    JSONParser jParser=new JSONParser(); 

    ArrayList<HashMap<String,String>> currentRates; 

    JSONArray rates=null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     currentRates=new ArrayList<HashMap<String,String>>(); 

     new getRates().execute(); 

     ListView lv=getListView(); 


    } 

    public class getRates extends AsyncTask<String,String,String>{ 

    @Override 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     dialog=new ProgressDialog(MainActivity.this); 
     dialog.setMessage("Please wait"); 
     dialog.setIndeterminate(false); 
     dialog.setCancelable(false); 
     dialog.show(); 
    } 

     @Override 
     protected String doInBackground(String... args) { 

List<NameValuePair> params=new ArrayList<NameValuePair>(); 

JSONObject json=jParser.makeHttpRequest(url,"GET",params); 

Log.d("Current Rates:",json.toString()); 

try { 
    rates=json.getJSONArray(RATES_TAG); 

    for(int i=0;i<rates.length();i++){ 
     JSONObject c=rates.getJSONObject(i); 


     String rates=c.getString(RATES_TAG); 

     HashMap<String,String>map=new HashMap<String,String>(); 

     map.put(RATES_TAG, rates); 

     currentRates.add(map); 
    } 
} catch (JSONException e){ 
    e.printStackTrace(); 
} 

      return null; 
     } 

     protected void onPostExecute(String url){ 
      dialog.dismiss(); 

      runOnUiThread(new Runnable(){ 
       public void run(){ 
        ListAdapter adapter=new SimpleAdapter(MainActivity.this, currentRates ,R.layout.rates_item,new String[]{RATES_TAG},new int[]{R.id.rates}); 

        setListAdapter(adapter); 
       } 
      }); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 


} 

這裏是我的logcat報告:

> 07-08 19:46:13.103: E/AndroidRuntime(331): FATAL EXCEPTION: AsyncTask 
> #1 07-08 19:46:13.103: E/AndroidRuntime(331): java.lang.RuntimeException: An error occured while executing 
> doInBackground() 07-08 19:46:13.103: E/AndroidRuntime(331): at 
> android.os.AsyncTask$3.done(AsyncTask.java:200) 07-08 19:46:13.103: 
> E/AndroidRuntime(331): at 
> java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
> 07-08 19:46:13.103: E/AndroidRuntime(331): at 
> java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
> 07-08 19:46:13.103: E/AndroidRuntime(331): at 
> java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
> 07-08 19:46:13.103: E/AndroidRuntime(331): at 
> java.util.concurrent.FutureTask.run(FutureTask.java:138) 07-08 
> 19:46:13.103: E/AndroidRuntime(331): at 
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
> 07-08 19:46:13.103: E/AndroidRuntime(331): at 
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
> 07-08 19:46:13.103: E/AndroidRuntime(331): at 
> java.lang.Thread.run(Thread.java:1019) 07-08 19:46:13.103: 
> E/AndroidRuntime(331): Caused by: java.lang.NullPointerException 07-08 
> 19:46:13.103: E/AndroidRuntime(331): at 
> com.razzil.currency.MainActivity$getRates.doInBackground(MainActivity.java:69) 
> 07-08 19:46:13.103: E/AndroidRuntime(331): at 
> com.razzil.currency.MainActivity$getRates.doInBackground(MainActivity.java:1) 
> 07-08 19:46:13.103: E/AndroidRuntime(331): at 
> android.os.AsyncTask$2.call(AsyncTask.java:185) 07-08 19:46:13.103: 
> E/AndroidRuntime(331): at 
> java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
> 07-08 19:46:13.103: E/AndroidRuntime(331): ... 4 more 07-08 
> 19:46:15.372: E/WindowManager(331): Activity 
> com.razzil.currency.MainActivity has leaked window 
> [email protected] that 
> was originally added here 07-08 19:46:15.372: E/WindowManager(331): 
> android.view.WindowLeaked: Activity com.razzil.currency.MainActivity 
> has leaked window 
> [email protected] that 
> was originally added here 07-08 19:46:15.372: E/WindowManager(331): 
> at android.view.ViewRoot.<init>(ViewRoot.java:258) 07-08 
> 19:46:15.372: E/WindowManager(331): at 
> android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 
> 07-08 19:46:15.372: E/WindowManager(331):  at 
> android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
> 07-08 19:46:15.372: E/WindowManager(331):  at 
> android.view.Window$LocalWindowManager.addView(Window.java:424) 07-08 
> 19:46:15.372: E/WindowManager(331): at 
> android.app.Dialog.show(Dialog.java:241) 07-08 19:46:15.372: 
> E/WindowManager(331):  at 
> com.razzil.currency.MainActivity$getRates.onPreExecute(MainActivity.java:59) 
> 07-08 19:46:15.372: E/WindowManager(331):  at 
> android.os.AsyncTask.execute(AsyncTask.java:391) 07-08 19:46:15.372: 
> E/WindowManager(331):  at 
> com.razzil.currency.MainActivity.onCreate(MainActivity.java:43) 07-08 
> 19:46:15.372: E/WindowManager(331): at 
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
> 07-08 19:46:15.372: E/WindowManager(331):  at 
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
> 07-08 19:46:15.372: E/WindowManager(331):  at 
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
> 07-08 19:46:15.372: E/WindowManager(331):  at 
> android.app.ActivityThread.access$1500(ActivityThread.java:117) 07-08 
> 19:46:15.372: E/WindowManager(331): at 
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
> 07-08 19:46:15.372: E/WindowManager(331):  at 
> android.os.Handler.dispatchMessage(Handler.java:99) 07-08 
> 19:46:15.372: E/WindowManager(331): at 
> android.os.Looper.loop(Looper.java:123) 07-08 19:46:15.372: 
> E/WindowManager(331):  at 
> android.app.ActivityThread.main(ActivityThread.java:3683) 07-08 
> 19:46:15.372: E/WindowManager(331): at 
> java.lang.reflect.Method.invokeNative(Native Method) 07-08 
> 19:46:15.372: E/WindowManager(331): at 
> java.lang.reflect.Method.invoke(Method.java:507) 07-08 19:46:15.372: 
> E/WindowManager(331):  at 
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
> 07-08 19:46:15.372: E/WindowManager(331):  at 
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 07-08 
> 19:46:15.372: E/WindowManager(331): at 
> dalvik.system.NativeStart.main(Native Method) 
+0

正在運行成**空指針異常**這裏的關鍵行是 _E/AndroidRuntime(331):07-08顯示java.lang.NullPointerException 19:46:13.103:致È/AndroidRuntime(331):at com.razzil.currency.MainActivity $ getRates.doInBackground(MainActivity.java:69)_ – Abhijit 2012-07-08 14:33:39

+0

我想這裏'Log.d(「Current Rates:」,json.toString());'json一片空白! – 2012-07-08 14:59:10

回答

2

你打NullPointerException異常在MainActivity 號線69。它在doInBackground方法中。調試它並查看哪些值爲空並導致問題。

> java.lang.Thread.run(Thread.java:1019) 07-08 19:46:13.103: 
> E/AndroidRuntime(331): Caused by: java.lang.**NullPointerException** 07-08 
> 19:46:13.103: E/AndroidRuntime(331): at 
> com.razzil.currency.MainActivity$getRates.doInBackground(MainActivity.java:69) 
> 07-08 19:46:13.103: E/AndroidRuntime(331): at 
> com.razzil.currency.MainActivity$getRates.doInBackground(MainActivity.java:1) 
> 07-08 19:46:13.103: E/AndroidRuntime(331):