0

単純なクラスでアラートボックスを作成しましたが、何が問題なのかわかりません。私のコードを以下に示します。アクティビティを実行し、alertdialog を実行したい場合、アプリケーションがクラッシュします。

private class ApplicationLauncher implements
            AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(final AdapterView parent, View v,
                final int position, long id) {
            // //////////////////////////////////////////////////////////
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    getApplicationContext());
            builder.setCancelable(true);
            builder.setTitle("TestsAuthen");
            builder.setInverseBackgroundForced(true);
            builder.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ApplicationInfo app = (ApplicationInfo) parent
                                    .getItemAtPosition(position);
                            startActivity(app.intent);
                        }
                    });
            builder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();

            // //////////////////
        }
    }
4

2 に答える 2

0

クラスにコンストラクターを追加して、ローカル コンテキストを初期化します。

public class ApplicationLauncher implements AdapterView.OnItemClickListener {

private Context context;

public ApplicationLauncher(Context context) {
    this.context = context;
}
...

}

アクティビティでこのクラスを次のようにインスタンス化します。

 ApplicationLauncher al = new ApplicationLauncher( this );
于 2013-08-27T10:13:38.470 に答える