私は懐中電灯プロジェクトに取り組んでいます。別のクラスを使用してこれを行いたいです。
ここに私の 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が発生します。なぜこのエラーが発生するのですか? 理解できない。エラーログ出力を見てください。