0

我在尝试使用按钮 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: {}]
4

2 回答 2

0

根据您的 logcat 它在 onpostexecute() 之前是完美的,我认为您正在等待从以下 url 获得一些结果,这会导致延迟并完成您的活动。

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

于 2013-04-03T02:35:06.320 回答
0

我解决了这个问题。该应用程序崩溃了,因为我编写了要使用 main.xml 布局显示的代码,但一直在尝试使用第二个界面来控制 main.xml 界面。这导致了助手活动的复杂性。

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

于 2013-04-09T20:34:20.910 回答