Android手电筒崩溃主动

时间:2014-08-19 11:01:54

标签: android crash flashlight

嗨,大家好我正在尝试为我的Android手机开发手电筒应用程序,但是当我运行它时,我得到了错误。 GUI有2个按钮:1个用于打开灯,另一个用于关闭灯

这是代码:

package com.example.flash;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class Flash extends ActionBarActivity {


    private final static String LOG_TAG = "FlashLight";

    private Button mOnBtn;
    private Button mOffBtn;

    private Camera mCamera;


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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        mOnBtn = (Button) findViewById(R.id.onButton);
        mOnBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                processOnClick();
            }
        });

        mOffBtn = (Button) findViewById(R.id.offButton);
        mOffBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                processOffClick();
            }
        });
    }

    private void processOffClick(){
        if( mCamera != null ){
            Parameters params = mCamera.getParameters();
            params.setFlashMode( Parameters.FLASH_MODE_OFF );
            mCamera.setParameters( params );
        }
    }

    private void processOnClick(){
        if( mCamera != null ){
            Parameters params = mCamera.getParameters();
            params.setFlashMode( Parameters.FLASH_MODE_TORCH );
            mCamera.setParameters( params );
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.flash, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_flash,
                    container, false);
            return rootView;
        }
    }

}

我在androidmanifest.xml中添加了权限

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.flash.Flash"
            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>

这里有logcat

    08-19 12:58:54.426: E/AndroidRuntime(17639): FATAL EXCEPTION: main
08-19 12:58:54.426: E/AndroidRuntime(17639): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flash/com.example.flash.Flash}: java.lang.NullPointerException
08-19 12:58:54.426: E/AndroidRuntime(17639):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at android.os.Looper.loop(Looper.java:137)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at java.lang.reflect.Method.invoke(Method.java:525)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at dalvik.system.NativeStart.main(Native Method)
08-19 12:58:54.426: E/AndroidRuntime(17639): Caused by: java.lang.NullPointerException
08-19 12:58:54.426: E/AndroidRuntime(17639):    at com.example.flash.Flash.onCreate(Flash.java:40)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at android.app.Activity.performCreate(Activity.java:5133)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-19 12:58:54.426: E/AndroidRuntime(17639):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-19 12:58:54.426: E/AndroidRuntime(17639):    ... 11 more

我不知道出了什么问题......请帮助我

fragment_flash.xml代码是这样的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4F4F4F"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.flash.Flash$PlaceholderFragment" >

    <Button
        android:id="@+id/onButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="93dp"
        android:layout_marginTop="213dp"
        android:text="On" />

    <Button
        android:id="@+id/offButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/onButton"
        android:layout_alignBottom="@+id/onButton"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/onButton"
        android:text="Off" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

试试这个..

改变这个..

setContentView(R.layout.activity_flash);

setContentView(R.layout.fragment_flash);

因为Button视图必须仅引用特定布局

删除PlaceholderFragment.class

然后也删除

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

修改

内部processOnClick()方法

            Camera camera = Camera.open();
            camera.startPreview();
            Parameters params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();