1

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

各位看官有没有看错?

提前致谢!

这是代码;

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));

        }


    }
}

日志猫:

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

主要的.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);
            }
        });
4

2 回答 2

2

你的错误是我认为在你面前。Button你也给了错误的id ImageView

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

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

你的ImageView身份证R.id.imageView1如下R.id.chosenPicView

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

然后试一试。

于 2013-03-17T10:11:02.350 回答
1

只是改变

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

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

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

总是仔细检查你的身份证。

于 2013-03-17T10:12:47.070 回答