0

我正在做一个手电筒项目。我想通过使用另一个类来做到这一点。

这是我的 mainactivity.java 文件:

package com.efsunderin.callcamera;

import android.hardware.Camera;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
    public class MainActivity extends Activity {


Button bFlash;
Button cFlash;
MyCamera bnmcamera;
Camera camera;

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

bFlash = (Button)findViewById(R.id.btnFlash);
cFlash = (Button)findViewById(R.id.closeFlash);

bFlash.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        bnmcamera.getCamera();
        bnmcamera.turnOnFlash();

    }
});


cFlash.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        bnmcamera.turnOffFlash();

    }
});

}


@Override
protected void onStop() {
    super.onStop();

    // on stop release the camera
    if (camera != null) {
        camera.release();
        camera = null;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

在此活动中,我将调用以下课程:

package com.efsunderin.callcamera;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;

public class MyCamera {



Camera camera;
Parameters params;


public void getCamera() {


    camera.open();
    params = camera.getParameters();
    camera.startPreview();

}

public void turnOnFlash() {

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

}

public void turnOffFlash() {

    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.startPreview();

}


}

但它不起作用,当我触摸按钮打开闪光灯时系统关闭了应用程序。我究竟做错了什么?请帮忙。谢谢。


这是logcat *输出: *

    08-31 08:53:22.157: D/AndroidRuntime(287): CheckJNI is ON
08-31 08:53:22.347: D/AndroidRuntime(287): --- registering native functions ---
08-31 08:53:23.247: D/dalvikvm(218): GC_EXPLICIT freed 406 objects / 23512 bytes in 68ms
08-31 08:53:23.367: D/PackageParser(58): Scanning package: /data/app/vmdl50609.tmp
08-31 08:53:23.586: I/PackageManager(58): Removing non-system package:com.eeecoder.callcamera
08-31 08:53:23.586: I/ActivityManager(58): Force stopping package com.eeecoder.callcamera uid=10049
08-31 08:53:23.777: D/PackageManager(58): Scanning package com.eeecoder.callcamera
08-31 08:53:23.777: I/PackageManager(58): Package com.eeecoder.callcamera codePath changed from /data/app/com.eeecoder.callcamera-1.apk to /data/app/com.eeecoder.callcamera-2.apk; Retaining data and using new
08-31 08:53:23.787: I/PackageManager(58): /data/app/com.eeecoder.callcamera-2.apk changed; unpacking
08-31 08:53:23.807: D/installd(34): DexInv: --- BEGIN '/data/app/com.eeecoder.callcamera-2.apk' ---
08-31 08:53:25.380: D/dalvikvm(294): DexOpt: load 205ms, verify 971ms, opt 42ms
08-31 08:53:25.427: D/installd(34): DexInv: --- END '/data/app/com.eeecoder.callcamera-2.apk' (success) ---
08-31 08:53:25.427: W/PackageManager(58): Code path for pkg : com.eeecoder.callcamera changing from /data/app/com.eeecoder.callcamera-1.apk to /data/app/com.eeecoder.callcamera-2.apk
08-31 08:53:25.427: W/PackageManager(58): Resource path for pkg : com.eeecoder.callcamera changing from /data/app/com.eeecoder.callcamera-1.apk to /data/app/com.eeecoder.callcamera-2.apk
08-31 08:53:25.427: D/PackageManager(58):   Activities: com.eeecoder.callcamera.MainActivity
08-31 08:53:25.447: I/ActivityManager(58): Force stopping package com.eeecoder.callcamera uid=10049
08-31 08:53:25.667: I/installd(34): move /data/dalvik-cache/data@app@com.eeecoder.callcamera-2.apk@classes.dex -> /data/dalvik-cache/data@app@com.eeecoder.callcamera-2.apk@classes.dex
08-31 08:53:25.667: D/PackageManager(58): New package installed in /data/app/com.eeecoder.callcamera-2.apk
08-31 08:53:25.897: I/ActivityManager(58): Force stopping package com.eeecoder.callcamera uid=10049
08-31 08:53:26.077: D/dalvikvm(58): GC_EXPLICIT freed 12492 objects / 745576 bytes in 179ms
08-31 08:53:26.277: D/dalvikvm(123): GC_EXPLICIT freed 7018 objects / 337856 bytes in 232ms
08-31 08:53:26.857: W/RecognitionManagerService(58): no available voice recognition services found
08-31 08:53:27.067: D/dalvikvm(150): GC_EXPLICIT freed 3878 objects / 211624 bytes in 638ms
08-31 08:53:27.212: D/dalvikvm(58): GC_EXPLICIT freed 5013 objects / 274168 bytes in 119ms
08-31 08:53:27.217: I/installd(34): unlink /data/dalvik-cache/data@app@com.eeecoder.callcamera-1.apk@classes.dex
08-31 08:53:27.267: D/AndroidRuntime(287): Shutting down VM
08-31 08:53:27.287: D/dalvikvm(287): Debugger has detached; object registry had 1 entries
08-31 08:53:27.307: I/AndroidRuntime(287): NOTE: attach of thread 'Binder Thread #3' failed
08-31 08:53:27.857: D/AndroidRuntime(299): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
08-31 08:53:27.857: D/AndroidRuntime(299): CheckJNI is ON
08-31 08:53:28.127: D/AndroidRuntime(299): --- registering native functions ---
08-31 08:53:29.177: I/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.eeecoder.callcamera/.MainActivity }
08-31 08:53:29.267: D/AndroidRuntime(299): Shutting down VM
08-31 08:53:29.297: D/dalvikvm(299): Debugger has detached; object registry had 1 entries
08-31 08:53:29.337: I/ActivityManager(58): Start proc com.eeecoder.callcamera for activity com.eeecoder.callcamera/.MainActivity: pid=306 uid=10049 gids={1006}
08-31 08:53:29.387: I/dalvikvm(299): JNI: AttachCurrentThread (from ???.???)
08-31 08:53:29.387: I/AndroidRuntime(299): NOTE: attach of thread 'Binder Thread #3' failed
08-31 08:53:30.257: I/ActivityManager(58): Displayed activity com.eeecoder.callcamera/.MainActivity: 989 ms (total 989 ms)
08-31 08:53:35.357: D/dalvikvm(123): GC_EXPLICIT freed 965 objects / 55776 bytes in 68ms
08-31 08:53:40.367: D/dalvikvm(218): GC_EXPLICIT freed 117 objects / 5144 bytes in 72ms
08-31 08:53:45.377: D/dalvikvm(257): GC_EXPLICIT freed 759 objects / 55000 bytes in 80ms
08-31 08:54:31.157: D/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol

这是当我触摸它提供这些输出并被系统停止的按钮时的错误日志输出:

错误日志:

    08-31 09:55:19.347: E/AndroidRuntime(357): FATAL EXCEPTION: main
08-31 09:55:19.347: E/AndroidRuntime(357): java.lang.NullPointerException
08-31 09:55:19.347: E/AndroidRuntime(357):  at com.eeecoder.callcamera.MyCamera.getCamera(MyCamera.java:24)
08-31 09:55:19.347: E/AndroidRuntime(357):  at com.eeecoder.callcamera.MainActivity$1.onClick(MainActivity.java:35)
08-31 09:55:19.347: E/AndroidRuntime(357):  at android.view.View.performClick(View.java:2408)
08-31 09:55:19.347: E/AndroidRuntime(357):  at android.view.View$PerformClick.run(View.java:8816)
08-31 09:55:19.347: E/AndroidRuntime(357):  at android.os.Handler.handleCallback(Handler.java:587)
08-31 09:55:19.347: E/AndroidRuntime(357):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-31 09:55:19.347: E/AndroidRuntime(357):  at android.os.Looper.loop(Looper.java:123)
08-31 09:55:19.347: E/AndroidRuntime(357):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-31 09:55:19.347: E/AndroidRuntime(357):  at java.lang.reflect.Method.invokeNative(Native Method)
08-31 09:55:19.347: E/AndroidRuntime(357):  at java.lang.reflect.Method.invoke(Method.java:521)
08-31 09:55:19.347: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-31 09:55:19.347: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-31 09:55:19.347: E/AndroidRuntime(357):  at dalvik.system.NativeStart.main(Native Method)

我初始化了所需的对象,但它仍然给出NullPointerException,为什么它会给出这个错误?我不明白。请查看错误日志输出。

4

2 回答 2

0

在调用该方法之前,您忘记先创建对象。

干得好:

private MyCamera=null;

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

    **MyCamera bnmcamera=new MyCamera();**

bFlash = (Button)findViewById(R.id.btnFlash);
cFlash = (Button)findViewById(R.id.closeFlash);

bFlash.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        bnmcamera.getCamera();
        bnmcamera.turnOnFlash();

    }
});
于 2013-08-31T06:33:19.033 回答
0

您尚未初始化变量 bnmCamera 和相机。

MyCamera bnmcamera;
Camera camera;

初始化这些变量,然后使用它。如果你使用变量而不初始化它会给你NullPointerException.

我想你想这样做:(你没有在任何地方使用过相机对象)

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

bFlash = (Button)findViewById(R.id.btnFlash);
cFlash = (Button)findViewById(R.id.closeFlash);
//initialize bnmcamera
bnmcamera = new MyCamera();

bFlash.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        bnmcamera.getCamera();
        bnmcamera.turnOnFlash();

    }
});


cFlash.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        bnmcamera.turnOffFlash();

    }
});

}

相机类:

您不必每次都开始相机预览。如果你想这样做,你有停止预览如果它已经开始释放相机然后重新开始。

检查此链接相机预览演示

编辑: 试试这个:

public void getCamera() {


    camera = camera.open();
    params = camera.getParameters();
    camera.startPreview();

}
public void turnOnFlash() {
    if(camera != null){
     params = camera.getParameters();
     params.setFlashMode(Parameters.FLASH_MODE_TORCH);
     camera.setParameters(params);
     camera.startPreview();
    }else{
    Log.e("CHECK","Camera null");
    }

}

在所有功能中执行此检查。

于 2013-08-31T07:43:37.923 回答