我正在学习安卓摄像头捕获并将其设置为教程中的壁纸,但是收到错误我努力寻找但是无法弄明白我将该课程设置在另一个课程中另一个列表点击这里是我的代码。
public class Camera extends Activity implements View.OnClickListener {
ImageView iv;
ImageButton ib;
Button b;
Intent i;
final static int cameraData = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
initilize();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.BResults:
break;
case R.id.ibtakenimage:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("Data");
iv.setImageBitmap(bmp);
}
}
public void initilize() {
iv = (ImageView) findViewById(R.id.IVimageview);
ib = (ImageButton) findViewById(R.id.ibtakenimage);
b = (Button) findViewById(R.id.BResults);
b.setOnClickListener(this);
ib.setOnClickListener(this);
}
}
这是我的logcat。
01-14 02:37:21.770: E/Trace(1247): error opening trace file: No such file or directory (2)
01-14 02:37:22.200: D/dalvikvm(1247): GC_FOR_ALLOC freed 79K, 9% free 2442K/2660K, paused 55ms, total 57ms
01-14 02:37:22.210: I/dalvikvm-heap(1247): Grow heap (frag case) to 3.103MB for 614416-byte allocation
01-14 02:37:22.250: D/dalvikvm(1247): GC_FOR_ALLOC freed 2K, 7% free 3040K/3264K, paused 35ms, total 35ms
01-14 02:37:22.280: D/dalvikvm(1247): GC_CONCURRENT freed <1K, 7% free 3190K/3416K, paused 4ms+3ms, total 38ms
01-14 02:37:22.520: D/gralloc_goldfish(1247): Emulator without GPU emulation detected.
01-14 02:37:28.100: I/Choreographer(1247): Skipped 195 frames! The application may be doing too much work on its main thread.
01-14 02:37:28.270: I/Choreographer(1247): Skipped 30 frames! The application may be doing too much work on its main thread.
01-14 02:37:43.140: I/Choreographer(1247): Skipped 52 frames! The application may be doing too much work on its main thread.
01-14 02:37:43.440: D/AndroidRuntime(1247): Shutting down VM
01-14 02:37:43.460: W/dalvikvm(1247): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
----->>>> 01-14 02:37:43.530: E/AndroidRuntime(1247): FATAL EXCEPTION: main
01-14 02:37:43.530: E/AndroidRuntime(1247): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidme/com.example.androidme.Camera}: java.lang.NullPointerException
01-14 02:37:43.530: E/AndroidRuntime(1247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-14 02:37:43.530: E/AndroidRuntime(1247): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-14 02:37:43.530: E/AndroidRuntime(1247): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-14 02:37:43.530: E/AndroidRuntime(1247): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-14 02:37:43.530: E/AndroidRuntime(1247): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 02:37:43.530: E/AndroidRuntime(1247): at android.os.Looper.loop(Looper.java:137)
01-14 02:37:43.530: E/AndroidRuntime(1247): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-14 02:37:43.530: E/AndroidRuntime(1247): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 02:37:43.530: E/AndroidRuntime(1247): at java.lang.reflect.Method.invoke(Method.java:511)
01-14 02:37:43.530: E/AndroidRuntime(1247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-14 02:37:43.530: E/AndroidRuntime(1247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-14 02:37:43.530: E/AndroidRuntime(1247): at dalvik.system.NativeStart.main(Native Method)
01-14 02:37:43.530: E/AndroidRuntime(1247): Caused by: java.lang.NullPointerException
01-14 02:37:43.530: E/AndroidRuntime(1247): at com.example.androidme.Camera.initilize(Camera.java:58)
01-14 02:37:43.530: E/AndroidRuntime(1247): at com.example.androidme.Camera.onCreate(Camera.java:24)
01-14 02:37:43.530: E/AndroidRuntime(1247): at android.app.Activity.performCreate(Activity.java:5104)
01-14 02:37:43.530: E/AndroidRuntime(1247): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-14 02:37:43.530: E/AndroidRuntime(1247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-14 02:37:43.530: E/AndroidRuntime(1247): ... 11 more
----&GT;错误开始。
答案 0 :(得分:0)
Caused by: java.lang.NullPointerException
at com.example.androidme.Camera.initilize(Camera.java:58)
告诉你错误。在Camera类的第58行中,您正在尝试访问不存在的对象,即为null。
必须是以下其中一条:
b.setOnClickListener(this);
ib.setOnClickListener(this);
所以你要做的就是检查你之前输入的ID是否与你在.xml中指定的ID一致。