onClick在执行AsyncTask时崩溃应用程序

时间:2013-04-02 19:17:38

标签: android crash onclick android-asynctask

我正在尝试使用onClick事件按钮运行的AsyncTask有些麻烦。我让它在“HomePageActivity”中工作,但随后将代码转移到一个新活动中,现在应用程序在单击按钮时崩溃。它曾经完美地运作。

任何人都可以帮我解决这个问题吗? 我确信一双新眼睛可以发现它很简单。

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: {}]

2 个答案:

答案 0 :(得分:0)

根据你的logcat它的完美直到onpostexecute(), 我想你正等着从下面的网址得到一些结果 这导致延迟和完成你的活动。

http://football.myfantasyleague.com/2007/export?TYPE=topAdds&W=12&JSON=1?=

答案 1 :(得分:0)

我解决了这个问题。该应用程序崩溃是因为我编写了使用main.xml布局显示的代码,但一直试图使用第二个接口来控制main.xml接口。这导致帮助活动的并发症。

感谢所有试图帮助我解决这个问题的人。