Android从画廊NullPointerException中挑选图片?

时间:2013-03-17 09:58:24

标签: android image null

我目前正在尝试创建一个App,它允许用户从图库中拍摄图像并将其上传到服务器。但我仍然坚持选择一个图像。 我完全按照教程,但在启动活动时,我得到了一个N​​ullPointer。

你们看到有什么错误吗?

提前致谢!

继承代码;

package de.arvidg.exampleactionbartabs;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class UploadPicture extends Activity {

    private static int RESULT_LOAD_IMAGE = 1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button buttonLoadImage = (Button) findViewById(R.id.choosepic2);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.chosenPicView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


    }
}

logcat的:

03-17 10:56:35.470: W/dalvikvm(12169): threadid=1: thread exiting with uncaught exception (group=0x41e64300)
03-17 10:56:35.480: E/AndroidRuntime(12169): FATAL EXCEPTION: main
03-17 10:56:35.480: E/AndroidRuntime(12169): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.arvidg.exampleactionbartabs/de.arvidg.exampleactionbartabs.UploadPicture}: java.lang.NullPointerException
03-17 10:56:35.480: E/AndroidRuntime(12169):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at android.app.ActivityThread.access$600(ActivityThread.java:145)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at android.os.Looper.loop(Looper.java:137)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at android.app.ActivityThread.main(ActivityThread.java:4978)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at java.lang.reflect.Method.invokeNative(Native Method)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at java.lang.reflect.Method.invoke(Method.java:511)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at dalvik.system.NativeStart.main(Native Method)
03-17 10:56:35.480: E/AndroidRuntime(12169): Caused by: java.lang.NullPointerException
03-17 10:56:35.480: E/AndroidRuntime(12169):    at de.arvidg.exampleactionbartabs.UploadPicture.onCreate(UploadPicture.java:34)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at android.app.Activity.performCreate(Activity.java:5008)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-17 10:56:35.480: E/AndroidRuntime(12169):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-17 10:56:35.480: E/AndroidRuntime(12169):    ... 11 more

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/selectpic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="select Picture" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="225dp"
            android:layout_weight="29.60"
            android:src="@drawable/gradient_bg" />

        <TextView
            android:id="@+id/chosenpic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2.94"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>


</LinearLayout>

第34行是该声明的最后一个括号

buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });

2 个答案:

答案 0 :(得分:2)

你的错误是我想到你面前。 您还提供了ButtonImageView的错误ID。

使用您在布局中指定的ID初始化按钮,如下所示: 将您的按钮ID定义为R.id.selectpic

  Button buttonLoadImage = (Button) findViewById(R.id.selectpic);

除了ImageView之外,您的R.id.imageView1 ID为R.id.chosenPicView,如下所示:

 ImageView imageView = (ImageView) findViewById(R.id.imageView1); 

然后试试。

答案 1 :(得分:1)

只需更改

Button buttonLoadImage = (Button) findViewById(R.id.choosepic2);

Button buttonLoadImage = (Button) findViewById(R.id.selectpic);

在您的代码中的某个时刻,您更改了布局文件中按钮的ID,并且没有反映活动代码的更改。

总是仔细检查您的ID。