我的火炬应用中的错误

时间:2012-11-27 09:01:39

标签: java android eclipse

我正在尝试创建一个用于在Android上打开和关闭火炬的应用程序,我遇到了一些我无法解决的错误。这些错误似乎只出现在java活动中,似乎与我制作的'cam'活动有关。在我的java cam.release的底部,当我悬停它说'添加演员'。

感谢您提前帮助。

MainActivity.java

public class MainActivity extends Activity implements OnCheckedChangeListener {
    Camera cam;
    ToggleButton mTorch;
    Parameters camParams;
    private Context context;
    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    private final int FLASH_NOT_SUPPORTED = 0;
    private final int FLASH_TORCH_NOT_SUPPORTED = 1;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = MainActivity.this;
        if (context.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA_FLASH)) {
            mTorch = (ToggleButton) findViewById(R.id.toggleButton1);
        } else {           
             showDialog(MainActivity.this,FLASH_NOT_SUPPORTED);
        }
    }

    private void showDialog(MainActivity mainActivity, int fLASH_NOT_SUPPORTED2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        mTorch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @SuppressWarnings("deprecation")
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                try {
                    if (cam != null) {
                        cam = Camera.open();
                    }
                    camParams = cam.getParameters();
                    List<String> flashModes = camParams
                            .getSupportedFlashModes();
                    if (isChecked) {
                        if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
                            camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        } else {           
                             showDialog(MainActivity.this,FLASH_NOT_SUPPORTED);
                        }
                    } else {
                        camParams.setFlashMode(Parameters.FLASH_MODE_OFF);
                    }
                    cam.setParameters(camParams);
                    cam.startPreview();
                } catch (Exception e) {
                    e.printStackTrace();
                    cam.stopPreview();
                    cam.release();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (cam == null) {
            cam = Camera.open();
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (cam != null) {
            cam.release();
        }
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        cam.release();

    }
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.t.torch"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />

    <uses-feature
        android:name="android.hardware.Camera"
        android:required="true" >
    </uses-feature>
    <uses-feature
        android:name="android.hardware.camera.FLASHLIGHT"
        android:required="true" >
    </uses-feature>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.t.torch.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dip"
        android:text="@string/app_name"
        android:textSize="25sp" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dip"
        android:text="ToggleButton" >
    </ToggleButton>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您的主要问题是永远不会设置cam。您可能在与该变量相关的任何行上获得NullPointerExceptions。

原因是:

if (cam != null) {
    cam = Camera.open();
}

当然应该是:

if (cam == null) {
    cam = Camera.open();
}

另请注意,在调用cam时,onStop()可能为空。