检查设备是否支持手电筒后,不能将布尔值放入 if 语句中
After checking if device supports flashlight, can't put boolean value inside if statement
我检查了设备是否支持闪存并做了一个 if 语句,然后我收到了一个错误。有谁知道解决方案吗?它说 hasFlash
是一个未知的 class。
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();
}
}
对 AndroidManifest.xml
授予以下权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
将以下导入放在 MainActivity.java
之上
package com.Whosebug.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();
}
}
有关详细信息,请参阅 here
试试这个
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;
}
有问题的代码不在函数中。你的意思是把它放在你的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
我检查了设备是否支持闪存并做了一个 if 语句,然后我收到了一个错误。有谁知道解决方案吗?它说 hasFlash
是一个未知的 class。
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();
}
}
对 AndroidManifest.xml
授予以下权限<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
将以下导入放在 MainActivity.java
之上package com.Whosebug.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();
}
}
有关详细信息,请参阅 here
试试这个
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;
}
有问题的代码不在函数中。你的意思是把它放在你的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