【问题标题】:After checking if device supports flashlight, can't put boolean value inside if statement [duplicate]检查设备是否支持手电筒后,不能在 if 语句中放入布尔值 [重复]
【发布时间】:2016-11-04 21:03:13
【问题描述】:

我检查了设备是否支持闪存并且我做了一个 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();
}

}

【问题讨论】:

  • 错误是什么?
  • hasFlash == false可以简化为!hasFlash
  • 在同一行有多个错误:1. 意外令牌 2.未知类 `hasFlash 3. 需要识别
  • 嗯。 . .看起来你在代码的其他地方犯了一个错误。您应该发布整个文件,错误可能在您问题中当前代码的 sn-p 之外。
  • 发布了整个文件。

标签: java android android-package-managers flashlight


【解决方案1】:

有问题的代码不在函数中。你的意思是把它放在你的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

【讨论】:

  • 这解决了我的问题,非常感谢!
【解决方案2】:

试试这个

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;

}

【讨论】:

  • 这如何解决问题?
【解决方案3】:

在 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();
    }

}

更多详情请看here

【讨论】:

  • 感谢您的回答,但我已经在 AndroidManifest 中有这 2 个。
  • 在使用函数前在MainActivity.java上面添加import语句。
  • 错误显然不是权限或导入。
  • 您将代码放在 onCreate 函数之外。
猜你喜欢
  • 2019-06-30
  • 1970-01-01
  • 2020-03-02
  • 2018-09-03
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
  • 2013-01-04
相关资源
最近更新 更多