0

我的相机应用程序拍照并将媒体文件路径保存在一个字符串中,我用一个 stringextra (保存的路径)开始一个意图,我想用它来设置我的 imageviews 图像。

private void intentfbstart() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(this, FacebarActivity.class);
        intent.putExtra("com.lg.photoeveryday.CameraActivity.lastpicture",
                lastpicture);
        startActivityForResult(intent, 1);
        Log.i("APPLICATION", "STARTED INTENT");
    }

额外的包含字符串(= lastpicture),它是

mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
            lastpicture = mediaFile.getPath();

mediaFile 由 PictureCallback.onPictureTaken 使用

Intent 启动了一个新的 Activity,它应该使用 Intent-extra 来设置一个 imageviews 图像。

public class FacebarActivity extends Activity {

private ImageView imageview;
private ImageButton OKButton;
private Intent intent = getIntent();
private Bitmap picture;
private Toast toast;
private FacebarView fbview;
private ImageButton XButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pictureview);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    getWindow().setFormat(PixelFormat.UNKNOWN);

    ImageView imageview = (ImageView) findViewById(R.id.pictureviewLayout_pictureView);


    picture = BitmapFactory
            .decodeFile(intent
                    .getStringExtra("com.lg.photoeveryday.CameraActivity.lastpicture"));
    // picture = BitmapFactory.decodeResource(getResources(),
    // R.drawable.buttonplay); THIS WORKS, but with the intentextra it doesnt
    imageview.setImageBitmap(picture);

这就是 Logcat 抛出的:

01-26 14:53:39.425: I/APPLICATION(1543): STARTED INTENT
01-26 14:53:39.680: D/AndroidRuntime(1543): Shutting down VM
01-26 14:53:39.680: W/dalvikvm(1543): threadid=1: thread exiting with uncaught exception (group=0x410e62a0)
01-26 14:53:39.685: E/AndroidRuntime(1543): FATAL EXCEPTION: main
01-26 14:53:39.685: E/AndroidRuntime(1543): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lg.photoeveryday/com.lg.photoeveryday.FacebarActivity}: java.lang.NullPointerException
01-26 14:53:39.685: E/AndroidRuntime(1543):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at android.os.Looper.loop(Looper.java:137)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at android.app.ActivityThread.main(ActivityThread.java:4898)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at java.lang.reflect.Method.invokeNative(Native Method)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at java.lang.reflect.Method.invoke(Method.java:511)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at dalvik.system.NativeStart.main(Native Method)
01-26 14:53:39.685: E/AndroidRuntime(1543): Caused by: java.lang.NullPointerException
01-26 14:53:39.685: E/AndroidRuntime(1543):     at com.lg.photoeveryday.FacebarActivity.onCreate(FacebarActivity.java:50)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at android.app.Activity.performCreate(Activity.java:5206)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
01-26 14:53:39.685: E/AndroidRuntime(1543):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
01-26 14:53:39.685: E/AndroidRuntime(1543):     ... 11 more

logcat 抛出的行 (FacebarAcitvity.java:50) 是

picture = BitmapFactory
                .decodeFile(intent
                        .getStringExtra("com.lg.photoeveryday.CameraActivity.lastpicture"));

当我尝试使用资源中的位图时,它工作正常......

谢谢你的帮助

4

1 回答 1

0

FacebarActivity改变这一行

private Intent intent = getIntent();

private Intent intent = null;

然后在onCreate

intent = getIntent();

在方法返回onCreate之前getIntent()null

于 2013-01-26T14:14:58.917 回答