showNoCameraAlert错误

时间:2015-05-25 17:04:58

标签: android android-studio

我正在创建一个手电筒应用。在我的代码中,如果相机(硬件)在那里,它应该显示警报,但它不显示警报。我的代码如下:

if (!isCameraFlash) {
        showNoCameraAlert();
    } else {
        camera = Camera.open();
        params = camera.getParameters();
    }

但我在

上收到错误
showNoCameraAlert(); 

这是无效标签吗?或者我使用的教程是旧的?我正在使用android studio。这是我的完整Java代码:

public class MainActivity extends ActionBarActivity {

    private Camera camera;
    ImageButton flashlightSwitchImg;
    private boolean isFlashlightOn;
    Parameters params;

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

        flashlightSwitchImg = (ImageButton) findViewById(R.id.flashlightSwitch);
        boolean isCameraFlash = getApplicationContext().getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

        if (!isCameraFlash) {
            showNoCameraAlert();
        } else {
            camera = Camera.open();
            params = camera.getParameters();
        }
    }
}

任何帮助将不胜感激。谢谢:))

2 个答案:

答案 0 :(得分:1)

您正在调用showNoCameraAlert()方法。您需要将它添加到MainActivity(这是您放入代码的文件中。)所以像这样添加它:

private void showNoCameraAlert(){
    Toast.makeText(getApplicationContext(), "Camera flashlight not available in this Android device!", Toast.LENGTH_SHORT).show();
}

MainActivity的完整代码:

package com.example.administrator.flashlight;

import ...;

public class MainActivity extends ActionBarActivity {

    private Camera camera;
    ImageButton flashlightSwitchImg;
    private boolean isFlashlightOn;
    Parameters params;

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

        flashlightSwitchImg = (ImageButton) findViewById(R.id.flashlightSwitch);
        //check if phone has a flashlight
        boolean isCameraFlash = getApplicationContext().getPackageManager()    .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)

        if (!isCameraFlash) {
            //show error ( showNoCamereAlert() is below)
            showNoCameraAlert();
        } else {
            //open the camera
            camera = Camera.open();
            params = camera.getParameters();
        }
    }

    private void showNoCameraAlert(){
        //Show Error toast
        Toast.makeText(getApplicationContext(), "Camera flashlight not available in this Android device!", Toast.LENGTH_SHORT).show();
    }

}

答案 1 :(得分:-1)

我开始认为您没有实施showNoCameraAlert()方法。

尝试向MainActivity班级添加类似内容:

private void showNoCameraAlert(){
    Toast.makeText(getApplicationContext(), "There's no camera on the device or it doesn't have a flash", Toast.LENGTH_SHORT).show();
}