私はアンドロイドが初めてです。シンプルな Android アプリケーションを構築しようとしています: ユーザーがボタンをクリックすると、進行状況ダイアログが 5 秒間表示されます。ProgressDialog.show() を使用しましたが、context パラメーターに問題がありました。ここに私のxmlがあります:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnDialog2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnDialog2" />
</LinearLayout>
そして、ここに私のコードがあります:
public class Dialog22Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnDialog2 = (Button)findViewById(R.id.btnDialog2);
btnDialog2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog dialog = ProgressDialog.show(getBaseContext(),
"Progress dialog", "Loading...", true);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
dialog.dismiss();
} catch (InterruptedException e) {
}
}
}).start();
}
});
}
}
ProgressDialog.show() のコンテキスト パラメータを getBaseContext() から v.getContext() に変更すると、プログラムは正常に実行されます。だから私はここでコンテキストパラメータの意味は何ですか? 助けてくれてありがとう。