startActivity首先要求其startActivityForResult电话后叫 [英] startActivity calls first which is called after startActivityForResult call

查看:78
本文介绍了startActivity首先要求其startActivityForResult电话后叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个奇怪的结果,我从来没有期望。这可能是在这方面缺乏技能。

This is my first strange result that I never expected. This is may be lack of skill in that area.

嗯,我有一个按钮,通过它,我需要选择从手机的画廊(可能是从SD卡)的图像。我用隐含的意图拨打的电话长廊,得到了与 startActivityForResult绝对映像路径()。马上我打电话另一项活动把这个路径与 startActivity()

Well I had a button, through which I need to select an image from the phone gallery (probably from sdcard). I used implicit intent to call the phone gallery and got the absolute image path with startActivityForResult(). Immediately am calling another activity putting that path with startActivity().

根据我的情况我写了下面的code中的的onClick()按钮。

According to my scenario I wrote the following code in the onClick() of button.

@Override
public void onClick(View v) {
        upLoadPhoto();
}

protected void upLoadPhoto() {

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
    intent.setType("image/*");
    intent.putExtra("return-data", true);
    System.out.println("select image");
    startActivityForResult(intent, 1);
    startActivity(next);
    finish();
}

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == 1 && data != null && data.getData() != null){
                Uri uri = data.getData();
                if (uri != null) {
                    Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                    cursor.moveToFirst();
                    final String imageFilePath = cursor.getString(0);
                    System.out.println("Background : "+imageFilePath);
                    next.putExtra("backImagePath", imageFilePath);
                    cursor.close();
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

当我按一下按钮, startActivity(下)首先被调用,然后 startActivityForResult(意向,1)被调用。由于想获得图片路径通过捆绑对象中的第二个活动,我越来越 NullPointerException异常,因为 startActivity(下)被称为第一。

When I click the button, startActivity(next) is called first, then startActivityForResult(intent,1) is called. As am trying to get image path in the second activity through bundle object, am getting NullPointerException because of startActivity(next) is being called first.

我放弃了我的下巴,当我看到我的调试点并不如预期。希望我得到确切原因这个问题。

I dropped my jaws when I saw my debugging point are not as expected. Hope I get exact reason to this issue.

感谢
Aswin

Thanks
Aswin

推荐答案

那么移动呼叫 startActivity(下一个); onActivityResult( ) ...这种方式获得的路径之后,您将浏览到其他活动

What about moving the call to startActivity(next); into onActivityResult()... this way you will navigate to the other activity after getting the path

protected void upLoadPhoto() {

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("return-data", true);
System.out.println("select image");
startActivityForResult(intent, 1);

finish();

}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && data != null && data.getData() != null){
            Uri uri = data.getData();
            if (uri != null) {
                Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                cursor.moveToFirst();
                final String imageFilePath = cursor.getString(0);
                System.out.println("Background : "+imageFilePath);
                next.putExtra("backImagePath", imageFilePath);
                cursor.close();

            startActivity(next);

        super.onActivityResult(requestCode, resultCode, data);
    }
}

}

这篇关于startActivity首先要求其startActivityForResult电话后叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆