应用程序在几秒钟后关闭。我可以在logcat中看到JSON值,但结果在listview中没有更新。
我的主要活动类代码如下:
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
> com.android.internal.policy.impl.PhoneWindow$DecorView@40520830 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
> com.android.internal.policy.impl.PhoneWindow$DecorView@40520830 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 :(得分:2)
您正在MainActivity 第69行中点击 NULLPOINTEREXCEPTION 。它在doInBackground方法中。调试它,看看什么值为null并导致问题。
> 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):