警报按钮的DialogInterface.OnClickListener错误

时间:2015-03-11 23:18:19

标签: java android onclicklistener

当尝试在对话框按钮上设置单击侦听器时获取错误:类'从OnClickListener中取消的匿名类'必须在'OnClickListener'中声明为抽象或强制抽象方法'onClick(DialogInterface,int)'有人可以告诉我为什么这不起作用?

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.View;
import android.widget.Switch;


public class MainActivity extends Activity {

Switch swFlash;

private Camera camera;
private boolean isFlashOn = false;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Flash switch
    swFlash = (Switch) findViewById(R.id.swFlash);

    /*
     * First check if device supports flashlight
     */
    hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!hasFlash){
        // Device does not support flash
        // Show alert message and close the application
        AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
        alert.setTitle("Error");
        alert.setMessage("Sorry, your device does not support flashlight.");

        alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){
          void OnClick(DialogInterface alert, int which){
                //Close application
                finish();
            }
        });
        alert.show();
    return;
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

    AlertDialog alert = new AlertDialog.Builder(this).create();
    alert.setTitle("Error");
    alert.setMessage("Sorry, your device does not support flashlight.");
    ADB.setNeutralButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            finish();
            }
        });
    alert.show();