检查设备是否支持手电筒后,不能在if语句中输入布尔值



我检查了设备是否支持flash,我做了一个if语句,然后我得到了一个错误。有人知道解决方法吗?它说hasFlash是一个未知类。

import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraManager;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageButton;

import static android.content.DialogInterface.*;
public class Flashlight extends AppCompatActivity {
private CameraManager cm;
private ImageButton flashlightButton;
private boolean flashlightOnOrOff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flashlight);
    flashlightButton = (ImageButton) findViewById(R.id.flashOnOffButton);
    flashlightOnOrOff = false;
}
//Error if device does not have flashlight
boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
//This is where i get the error
if(hasFlash==false)
{
    AlertDialog dialo = new AlertDialog.Builder(Flashlight.this).create();
    dialo.setTitle("Error");
    dialo.setMessage("Sorry your device does not have flashlight");
    dialo.setButton(BUTTON_POSITIVE, "OK", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            finish();
        }
    });
    dialo.show();

}


//What flashlight does on Stop
@Override
protected void onStop() {
    super.onStop();
}
//What flashlight does on Pause
@Override
protected void onPause() {
    super.onPause();
}
//What flashlight does on Resume
@Override
protected void onPostResume() {
    super.onPostResume();
}

}

所讨论的代码不在函数中。你的意思是把它放在onCreate中吗?如果是,你的右括号在错误的地方:

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flashlight);
    flashlightButton = (ImageButton) findViewById(R.id.flashOnOffButton);
    flashlightOnOrOff = false; 
} // REMOVE THIS 
//Error if device does not have flashlight 
    boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    if(hasFlash==false)
    {
        AlertDialog dialo = new AlertDialog.Builder(Flashlight.this).create();
        dialo.setTitle("Error");
        dialo.setMessage("Sorry your device does not have flashlight");
        dialo.setButton(BUTTON_POSITIVE, "OK", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });
        dialo.show();
    }
} // THIS IS WHERE YOUR ONCREATE CLOSING BRACE GOES

试试这个

hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {    
// device doesn't 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 doesn't support flash light!");  
    alert.setButton("OK", new DialogInterface.OnClickListener() {    
    public void onClick(DialogInterface dialog, int which) {
        // closing the application    
        finish();    
    }
    });
    alert.show();    
    return;
}

在AndroidManifest.xml中设置以下权限

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

将以下导入置于MainActivity.java

之上
package com.stackoverflow.myapplication;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import static android.content.DialogInterface.BUTTON_POSITIVE;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        AlertDialog dlg = new AlertDialog.Builder(this).create();
        if (hasFlash) {
            dlg.setTitle("Done");
            dlg.setMessage("Your device does have flashlight");
        }
        else {
            dlg.setTitle("Error!");
            dlg.setMessage("Sorry your device does not have flashlight!");
        }
        dlg.setButton(BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //finish();
            }
        });
    dlg.show();
    }
}

更多详情请看这里

最新更新