CameraActivity中的NullPointerException

时间:2014-04-10 01:52:00

标签: java android eclipse android-intent android-activity

我想创建一个从相机拍摄照片的Activity。我遇到了一些问题并且不知道如何修复它,请帮助我。

我的xml:

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

    <ImageView
        android:id="@+id/imagePh"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/img1"
        android:visibility="gone" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btnPick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="15dp"
            android:text="@string/pick_pic" />

        <Button
            android:id="@+id/btnDoFoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="15dp"
            android:text="@string/make_photo" />
    </LinearLayout>

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstproject"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

</manifest>

活动

(我在youtube的课程中编写了这段代码,但是当我点击按钮在缩放活动上制作照片时,他没有工作。它是打开相机但是当你拍照并按下按钮时它就是#&# 39; s呼叫Force Close):

public class Scaling extends Activity implements OnClickListener {
    private static int LOAD_IM_RES = 1;
    private static int CAMERA_PIC = 1313;
    Button btnPick, btnMakePhoto;
    ImageView image, imagePh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scaling);
        btnMakePhoto = (Button) findViewById(R.id.btnDoFoto);
        btnPick = (Button) findViewById(R.id.btnPick);

        image = (ImageView) findViewById(R.id.image);
        image = (ImageView) findViewById(R.id.imagePh);

        btnMakePhoto.setOnClickListener(this);
        btnPick.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnPick:
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.
                        EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, LOAD_IM_RES);
                break;
            case R.id.btnDoFoto:
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                // request code

                startActivityForResult(cameraIntent, CAMERA_PIC);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == LOAD_IM_RES && resultCode == RESULT_OK && data != null) {
            Uri pickImage = data.getData();
            String[] filePath = {
                MediaStore.Images.Media.DATA
            };

            Cursor c = getContentResolver().query(pickImage, filePath, null, null, null);
            c.moveToFirst();

            String imagePath = c.getString(c.getColumnIndex(filePath[0]));
            image.setImageBitmap(BitmapFactory.decodeFile(imagePath));
            c.close();
        }
        if (requestCode == CAMERA_PIC && data != null) {
            Bundle extras = data.getExtras();
            Bitmap theImage = (Bitmap) extras.get("data");
            imagePh.setImageBitmap(theImage);
            imagePh.setVisibility(View.VISIBLE);
        }
    }

}

logcat的

04-10 01:40:29.593: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 52K, 53% free 2558K/5379K, external 1625K/2137K, paused 98ms
04-10 01:41:42.932: D/PhoneWindow(337): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@40538fb8 has no id.
04-10 01:41:46.111: W/IInputConnectionWrapper(337): showStatusIcon on inactive InputConnection
04-10 01:41:58.261: D/dalvikvm(337): GC_EXPLICIT freed 61K, 51% free 2667K/5379K, external 3733K/4044K, paused 2112ms
04-10 01:42:39.233: D/dalvikvm(337): GC_EXTERNAL_ALLOC freed 34K, 50% free 2715K/5379K, external 2615K/2673K, paused 87ms
04-10 01:42:39.262: D/AndroidRuntime(337): Shutting down VM
04-10 01:42:39.262: W/dalvikvm(337): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-10 01:42:39.301: E/AndroidRuntime(337): FATAL EXCEPTION: main
04-10 01:42:39.301: E/AndroidRuntime(337): java.lang.RuntimeException: Unable to resume activity {com.example.myfirstproject/com.example.myfirstproject.Tab}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=Scaling, request=1313, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.myfirstproject/com.example.myfirstproject.Tab}: java.lang.NullPointerException
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityThread.access$1600(ActivityThread.java:117)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.os.Looper.loop(Looper.java:123)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-10 01:42:39.301: E/AndroidRuntime(337):  at java.lang.reflect.Method.invokeNative(Native Method)
04-10 01:42:39.301: E/AndroidRuntime(337):  at java.lang.reflect.Method.invoke(Method.java:507)
04-10 01:42:39.301: E/AndroidRuntime(337):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-10 01:42:39.301: E/AndroidRuntime(337):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-10 01:42:39.301: E/AndroidRuntime(337):  at dalvik.system.NativeStart.main(Native Method)
04-10 01:42:39.301: E/AndroidRuntime(337): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=Scaling, request=1313, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.myfirstproject/com.example.myfirstproject.Tab}: java.lang.NullPointerException
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2107)
04-10 01:42:39.301: E/AndroidRuntime(337):  ... 13 more
04-10 01:42:39.301: E/AndroidRuntime(337): Caused by: java.lang.NullPointerException
04-10 01:42:39.301: E/AndroidRuntime(337):  at com.example.myfirstproject.Scaling.onActivityResult(Scaling.java:72)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:119)
04-10 01:42:39.301: E/AndroidRuntime(337):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
04-10 01:42:39.301: E/AndroidRuntime(337):  ... 14 more

我认为日期为空但是为什么以及如何解决它

1 个答案:

答案 0 :(得分:0)

imagePh永远不会绑定到图片,看起来你错过了它,将两张图片设置为image,而不是一张设置为imagePh,另一张设置为image。在onCreate中替换这些行,你应该没问题。

image = (ImageView) findViewById(R.id.image);
imagePh = findViewById(R.id.imagePh);