2012-08-10 121 views
1

我做了這個活動,任務沒有開始。我調試它,並onStart()去task.execute()線,但它永遠不會進入doInBackground()。在正在運行的任務列表中有一個AsyncTask,我認爲這是一個。我看不出問題在哪裏。這是代碼。活動不執行任務

package pl.edu.elka.pw.hosewski.wifi; 

public class NetDetail extends Activity implements OnClickListener{ 

private LineGraph lgNet; 
private WifiManager wifi; 
private WiFiDetail net; 
private DBSource dbs; 
private DBWifi dbw; 
private TextView ssid; 
private TextView bssid; 
private TextView freq; 
private TextView capab; 
private SharedPreferences getPrefs; 
private int interval; 
private LineRefresh task; 
private ViewSwitcher psSwitch; 
private Button bSwitch; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.netdetail); 
    dbs = new DBSource(this); 
    dbs.open(); 
    getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
    wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); 
    psSwitch = (ViewSwitcher) findViewById(R.id.psSwitch); 
    bSwitch = (Button) findViewById(R.id.bSwitch); 
    lgNet = (LineGraph) findViewById(R.id.lgNet); 
    lgNet.setClickable(true); 
    lgNet.setOnClickListener(this); 
    bSwitch.setOnClickListener(this); 
    Bundle b = getIntent().getExtras(); 
    long i = b.getLong("index"); 
    dbw = dbs.getWifi(i); 
    net = WiFiList.getDetailByBSSID(dbw.getBssid()); 
    lgNet.setDetail(net); 
    ssid = (TextView) findViewById(R.id.tvSSID); 
    bssid = (TextView) findViewById(R.id.tvBSSID); 
    freq = (TextView) findViewById(R.id.tvFrequen); 
    capab = (TextView) findViewById(R.id.tvCapab); 
    ssid.setText(dbw.getSsid()); 
    bssid.setText(dbw.getBssid()); 
    freq.setText(dbw.getFrequency() + " MHz"); 
    capab.setText(dbw.getCapability()); 
    this.setTitle(dbw.getSsid()+ " - detale"); 
    interval = Integer.parseInt(getPrefs.getString("intervals", "5")); 
    task = new LineRefresh(); 
} 


class LineRefresh extends AsyncTask<Void, Void, Void>{ 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 
     updLines(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     while(wifi.isWifiEnabled()){ 
      publishProgress(); 
      try { 
       Thread.sleep(interval); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      if(isCancelled()){ 
       break; 
      } 
     } 
     return null; 
    } 

} 

public void updLines(){ 
    lgNet.setDetail(net); 
    lgNet.postInvalidate(); 
} 

@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 
    dbs.close(); 
    task.cancel(true); 
} 


@Override 
protected void onStart() { 
    // TODO Auto-generated method stub 
    super.onStart(); 
    task.execute(); 
} 


@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()){ 
    case R.id.lgNet: 
     psSwitch.showNext(); 
     break; 
    case R.id.bSwitch: 
     psSwitch.showPrevious(); 
     break; 
    } 
} 
} 
+0

如果在doInBackground方法中放置日誌語句,會發生什麼情況? – Krylez 2012-08-10 22:33:27

+0

'net'永遠不會更新。它始終設置舊的舊值。 – zapl 2012-08-10 22:48:59

+0

Krylez:仍然沒有 – hubert 2012-08-11 08:10:39

回答

0

task = new LineRefresh()之後,加上​​。您可能需要也可能不需要「null」參數,請參閱Eclipse告訴您的內容。

+0

掀起一個錯誤 – hubert 2012-08-11 07:56:55

+0

我檢查了任務的狀態,如果它有助於找到解決方案,它將返回RUNNING。 – hubert 2012-08-11 17:56:13