0

我的代码是:

View.OnClickListener menuHandle = new View.OnClickListener() {
    public void onClick(View v) {
        //inflate menu

        final String [] items = new String[] {"Rate This App", "Quit"};
        final Integer[] icons = new Integer[] {R.drawable.star, R.drawable.quit};
        ListAdapter adapter = new ArrayAdapterWithIcon(MainActivity.this, items, icons);

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.DialogSlideAnim)
            .setAdapter(adapter, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item ) {
                //Toast.makeText(MainActivity.this, "Item Selected: " + item, Toast.LENGTH_SHORT).show();
                    switch (item) {
                    case 0:
                        //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        //Try Google play
                        intent.setData(Uri.parse("market://details?id=com.test.testing"));
                        if (MyStartActivity(intent) == false) {
                            //Market (Google play) app seems not installed, let's try to open a web browser
                            intent.setData(Uri.parse("https://play.google.com/store/apps/details?com.test.testing"));
                            if (MyStartActivity(intent) == false) {
                                //Well if this also fails, we have run out of options, inform the user.
                                //let the user know nothing was successful
                            }
                        }
                        break;
                    case 1:
                        finish();
                        break;
                    default:
                        //do nothing
                    }
                }
            });
        AlertDialog alert = builder.create();
        alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alert.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        alert.getWindow().setGravity(Gravity.BOTTOM);
        alert.show();
    }
};

我收到以下错误:

The constructor AlertDialog.Builder(MainActivity, int) is undefined

我必须修改什么才能消除错误?

注意:我的MainActivity类扩展ActivityDialogSlideAnimres/values/styles.xml文件中初始化

4

4 回答 4

2

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.DialogSlideAnim) 仅适用于 API 级别 11 及以上。

如果您针对所有平台,请使用以下。

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, R.style.DialogSlideAnim))

于 2013-10-01T12:50:55.120 回答
1

尝试

 new AlertDialog.Builder(v.getContext(), R.style.DialogSlideAnim)

代替

 new AlertDialog.Builder(MainActivity.this, R.style.DialogSlideAnim)
于 2013-10-01T12:50:26.317 回答
1

我认为这AlertDialog.Builder是因为在 a 内inner class(menuHandle.setOnClickListener),请尝试更改为:new AlertDialog.Builder(TheNameOfYourClass.this,R.style.DialogSlideAnim);

像这样:

public void onClick(View v) {
new AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.DialogSlideAnim);
^^^
 ....
}
于 2013-10-01T12:52:42.237 回答
1

尝试在您的样式中设置AlertDialog如下:

  new AlertDialog.Builder(
     new ContextThemeWrapper(MainActivity.this, R.style.DialogSlideAnim)
于 2013-10-01T12:52:58.433 回答