我遇到了一些AsyncTask問題,我嘗試使用按鈕onClick事件來運行。我曾在「HomePageActivity」中工作,但隨後將代碼轉移到新的活動中,現在該應用程序在單擊該按鈕時崩潰。它曾經完美地工作。onClick在執行AsyncTask時崩潰的應用程序
任何人都可以幫我解決這個問題嗎? 我相信這是一個新的眼睛可以發現的簡單。
public class GamesOverview extends HomePageActivity {
public final static String BaseUrl="http://URL.com/";
ArrayList<DEPT_HOLD> deptList=new ArrayList<DEPT_HOLD>();
private class GetDeptAyncTask extends AsyncTask<Hashtable<String,String>,Void,String>{
// Parse in background
@Override
protected String doInBackground(Hashtable<String,String>... params) {
@SuppressWarnings("rawtypes")
Hashtable ht=params[0];
@SuppressWarnings("unchecked")
String json=HelperHttp.getJSONResponseFromURL(BaseUrl+"JSONURLGOESHERE", ht);
if(json!=null) parseJsonString(deptList,json);
else {
return "Invalid Company Id";
}
return "SUCCESS";
}
// Parse JSON
protected void parseJsonString(ArrayList<DEPT_HOLD> deptList,String json) {
try {
JSONObject top = new JSONObject(json);
JSONObject topAdds = (JSONObject) top.get("topAdds");
JSONArray array = (JSONArray) topAdds.get("player");
for(int i = 0; i < array.length(); i++) {
JSONObject j = array.getJSONObject(i);
DEPT_HOLD d = new DEPT_HOLD();
d.one = j.optString("percent","");
d.two = j.optString("id","");
deptList.add(d);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Execute post
@Override
protected void onPostExecute(String result) {
if(result=="SUCCESS") {
Toast.makeText(GamesOverview.this, "Success"+deptList, Toast.LENGTH_SHORT).show();
DeptArrayAdapter adapter=new DeptArrayAdapter(GamesOverview.this,R.id.text1,deptList);
ListView listv=(ListView)findViewById(R.id.lv);
listv.setAdapter(adapter);
}
else{}
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Scrolling
ScrollView scroll;
scroll = new ScrollView(this);
HorizontalScrollView horizontalscroll;
horizontalscroll = new HorizontalScrollView(this);
// End
// Get JSON button
final Button JSONButton = new Button(this);
JSONButton.setText("Get JSON");
JSONButton.setTextSize(20);
JSONButton.setTextColor(Color.rgb(000,000,000));
JSONButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
executeAsyncTask();
}
});
// End
// LAYOUT
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(getWindowManager().getDefaultDisplay().getWidth(),getWindowManager().getDefaultDisplay().getHeight());
layout.setGravity(Gravity.CENTER_HORIZONTAL);
layout.setBackgroundResource(R.drawable.body_bg);
// DisplayJSONButton
LinearLayout displayJSONButtonlayout = new LinearLayout(this);
displayJSONButtonlayout.setOrientation(LinearLayout.HORIZONTAL);
displayJSONButtonlayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams displayJSONButtonlayout_params = new LinearLayout.LayoutParams(getWindowManager().getDefaultDisplay().getWidth()-50,100);
displayJSONButtonlayout_params.setMargins(0,10,0,0);
displayJSONButtonlayout.setGravity(Gravity.CENTER_HORIZONTAL);
displayJSONButtonlayout.addView(JSONButton,displayJSONButtonlayout_params);
// END LAYOUT
layout.addView(displayJSONButtonlayout);
layout.requestLayout();
// Enable scrolling
scroll.addView(layout,layoutparams);
horizontalscroll.addView(scroll);
// Set content view
setContentView(horizontalscroll);
}
// Finish up task
@SuppressWarnings("unchecked")
private void executeAsyncTask(){
Hashtable<String,String> ht=new Hashtable<String,String>();
GetDeptAyncTask async=new GetDeptAyncTask();
ht.put("", "");
@SuppressWarnings("rawtypes")
Hashtable[] ht_array={ht};
async.execute(ht_array);
}
}
所有的logcat的信息,我可以找到
:
?:??: W/?(?): URL==>http://football.myfantasyleague.com/2007/export?TYPE=topAdds&W=12&JSON=1?=
?:??: W/?(?): Forward DNS query to netd(h=football.myfantasyleague.com s=^)
?:??: W/?(?): at myfantasyleague.app.GamesOverview$GetDeptAyncTask.onPostExecute(GamesOverview.java:73)
?:??: W/?(?): at myfantasyleague.app.GamesOverview$GetDeptAyncTask.onPostExecute(GamesOverview.java:1)
?:??: W/?(?): Force finishing activity myfantasyleague.app/.GamesOverview
?:??: W/?(?): read: 04-02 14:42:47.990 464 474 W ActivityManager: Force finishing activity myfantasyleague.app/.GamesOverview
?:??: W/?(?): app: myfantasyleague.app
?:??: W/?(?): Activity pause timeout for ActivityRecord{41881918 myfantasyleague.app/.GamesOverview}
?:??: W/?(?): read: 04-02 14:42:48.521 464 477 W ActivityManager: Activity pause timeout for ActivityRecord{41881918 myfantasyleague.app/.GamesOverview}
?:??: W/?(?): ACTION_PACKAGE_REMOVED: myfantasyleague.app
?:??: W/?(?): dummy: {}]
?:??: W/?(?): 卸載了:myfantasyleague.app
?:??: W/?(?): myfantasyleague.app
?:??: W/?(?): PackageMonitor: doOnReceive(): android.intent.action.PACKAGE_REMOVED [myfantasyleague.app] REPLACING:true
?:??: W/?(?): dummy: {}]
?:??: W/?(?): PackageMonitor: doOnReceive(): android.intent.action.PACKAGE_REMOVED [myfantasyleague.app] REPLACING:true
?:??: W/?(?): PackageMonitor: doOnReceive(): android.intent.action.PACKAGE_ADDED [myfantasyleague.app] REPLACING:true
?:??: W/?(?): Activity idle timeout for ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}
?:??: W/?(?): PackageMonitor: doOnReceive(): android.intent.action.PACKAGE_ADDED [myfantasyleague.app] REPLACING:true
?:??: W/?(?): read: 04-02 14:42:58.560 464 477 W ActivityManager: Activity idle timeout for ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}
?:??: W/?(?): dummy: {}]
?:??: W/?(?): Activity pause timeout for ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}
?:??: W/?(?): read: 04-02 14:43:48.631 464 477 W ActivityManager: Activity pause timeout for ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}
?:??: W/?(?): dummy: {}]
?:??: W/?(?): Activity destroy timeout for ActivityRecord{41881918 myfantasyleague.app/.GamesOverview}
?:??: W/?(?): Activity stop timeout for ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}
?:??: W/?(?): read: 04-02 14:43:58.857 464 477 W ActivityManager: Activity destroy timeout for ActivityRecord{41881918 myfantasyleague.app/.GamesOverview}
?:??: W/?(?): read: 04-02 14:43:58.857 464 477 W ActivityManager: Activity stop timeout for ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}
?:??: W/?(?): This window was lost: Window{425b0c78 myfantasyleague.app/myfantasyleague.app.GamesOverview paused=false}
?:??: W/?(?): mToken=AppWindowToken{424bb208 token=Token{425d1e10 ActivityRecord{41881918 myfantasyleague.app/.GamesOverview}}}
?:??: W/?(?): mRootToken=AppWindowToken{424bb208 token=Token{425d1e10 ActivityRecord{41881918 myfantasyleague.app/.GamesOverview}}}
?:??: W/?(?): mAppToken=AppWindowToken{424bb208 token=Token{425d1e10 ActivityRecord{41881918 myfantasyleague.app/.GamesOverview}}}
?:??: W/?(?): mSurface=Surface(name=myfantasyleague.app/myfantasyleague.app.GamesOverview, identity=7073)
?:??: W/?(?): #16: Token{421d65e8 ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}}
?:??: W/?(?): #4: Window{42513558 myfantasyleague.app/myfantasyleague.app.MyFantasyLeagueActivity paused=false}
?:??: W/?(?): dummy: {}]
?:??: W/?(?): dummy: {}]
?:??: W/?(?): dummy: {}]
?:??: W/?(?): Activity idle timeout for ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}
?:??: W/?(?): read: 04-02 14:44:09.037 464 477 W ActivityManager: Activity idle timeout for ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}
?:??: W/?(?): WIN DEATH: Window{42513558 myfantasyleague.app/myfantasyleague.app.MyFantasyLeagueActivity paused=false}
?:??: W/?(?): Process myfantasyleague.app (pid 14733) has died.
?:??: W/?(?): read: 04-02 14:44:40.935 464 866 I ActivityManager: Process myfantasyleague.app (pid 14733) has died.
?:??: W/?(?): Force removing ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}: app died, no saved state
?:??: W/?(?): WIN DEATH: Window{425b0c78 myfantasyleague.app/myfantasyleague.app.GamesOverview paused=false}
?:??: W/?(?): read: 04-02 14:44:40.935 464 866 W ActivityManager: Force removing ActivityRecord{4184f330 myfantasyleague.app/.MyFantasyLeagueActivity}: app died, no saved state
?:??: W/?(?): dummy: {}]
什麼是logcat不得不說的呢? – codeMagic
你確定你的按鈕顯示..因爲你已經動態地創建了一個按鈕,並且你沒有將它添加到任何視圖,並且你甚至沒有把它作爲你的contentView保留下來 – Pragnani
Logcat似乎並沒有在我正在使用的程序。這是我看的第一個地方,但沒有出現這個崩潰。 – localhost