Android AlertDialog无法适合此设备

时间:2014-02-12 19:31:39

标签: android alertdialog

我正处于开发一些应用程序的开始,并且正在做一些类似于问题的概念验证,我遇到的问题开始令我感到沮丧:简单的AlertDialog。

这就是我正在做的事情:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button addLocationBtn = (Button) findViewById(R.id.addLocationBtn);
        addLocationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getApplicationContext());
alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                        result.setText(userInput.getText());
                        }
                      })
                    .setNegativeButton("Cancel",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                        }

                AlertDialog alertDialog = alertDialogBuilder.create();

                alertDialog.show();

            }
        });



    }

}

这是我得到的:

at [Source: java.io.StringReader@412f5d48; line: 1, column: 34]
02-12 19:44:53.008      748-748/com.cyanogenmod.trebuchet E/AppsCustomizePagedView﹕ Widget ComponentInfo{org.zooper.zwpro/org.zooper.zwlib.provider.WidgetProvider7x1} can not fit on this device (1032, 144)
02-12 19:44:53.008      748-748/com.cyanogenmod.trebuchet E/AppsCustomizePagedView﹕ Widget ComponentInfo{org.zooper.zwpro/org.zooper.zwlib.provider.WidgetProvider7x2} can not fit on this device (1032, 292)
02-12 19:44:53.048      748-748/com.cyanogenmod.trebuchet E/AppsCustomizePagedView﹕ Widget ComponentInfo{de.devmil.minimaltext/de.devmil.minimaltext.MinimalTextWidget_6_1} can not fit on this device (884, 144)
02-12 19:44:53.958  11114-11114/com.tepo.InTime E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

我做错了什么? Android SDK API 8

2 个答案:

答案 0 :(得分:2)

试试这个:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

不是

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());                             

result.setText(userInput.getText().toString());

不是

result.setText(userInput.getText());

答案 1 :(得分:-1)

我不确定这与您的错误有什么关系,但是你需要@Override你的.ClickPositiveButton和.setNegativeButton的onClicks。