startActivityForResult,附加问题

时间:2013-01-26 14:09:28

标签: android android-intent imageview logcat extra

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

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

extra包含字符串(= lastpicture),它是

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

mediaFile由PictureCallback.onPictureTaken

使用

意图启动一个新的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"));

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

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

FacebarActivity更改此行

private Intent intent = getIntent();

private Intent intent = null;

然后在onCreate

intent = getIntent();
onCreate getIntent()方法返回null

之前