我試圖從「Android的食譜」使用此代碼:我應該在Android環境中使用什麼?
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("FetchAndPopTask.doInBackground exception");
builder.setMessage(e.getMessage());
builder.setPositiveButton("OK", null);
builder.create().show();
...但不知道我應該替換爲「背景」。我已經嘗試過.java文件的類,直接類和「this」,但沒有編譯它們。
在更多的上下文,代碼:
public class SQLiteActivity extends ActionBarActivity {
private FetchAndPopTask _fetchAndPopTask;
. . .
private class FetchAndPopTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
. . .
try {
. . .
} catch (Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); // <= "context"...?
builder.setTitle("If I go blind, I'll use a Service Platypus (instead of a Service Dog)");
builder.setMessage(e.getMessage());
builder.setPositiveButton("OK", null);
builder.create().show();
return result;
}
我嘗試了所有的以下內容:
AlertDialog.Builder builder = new AlertDialog.Builder(SQLiteActivity);
AlertDialog.Builder builder = new AlertDialog.Builder(FetchAndPopTask);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
...但沒有編譯;那麼「背景」需要在這裏?
上下文中,您可以看到,異步任務類非靜態的,所以你可以使用「SQLiteActivity.this」更短 – Sam
@ SamN,一個我不知道。的確更短,更方便。謝謝;) –
不會說它更方便,因爲重構時可能會導致一些額外的工作(例如,當您將asynctask從此類中移出時)。然而它更短;) – Sam