我在android中通过fragmentdialog执行fileupload时遇到了问题。在测试我制作的activity等按钮以测试它之前,evrything工作正常,但由于我将它放在dilogfragment下,没有任何工作:

public class UploadF extends DialogFragment{

    ....

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {


    gallery_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent gal = new Intent();
            gal.setType("image/*");
            gal.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(gal, "Select Picture"), SELECT_IMAGE);

        }

    });




    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult( requestCode,  resultCode,  data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_IMAGE) {
                ....

                if ( selectedImagePath != null ) {
                    UploadFileI task = new UploadFileI( getActivity());
                    task.execute( url );
                }
            }



        }

    }



}

最佳答案

你说得对,什么都没发生。Activity有一种方法,也有一种方法。这可能会令人困惑,为了继续,请尝试通过调用以下命令使用活动上下文:

getActivity().startActivityForResult(Intent.createChooser(gal, "Select Picture"), SELECT_IMAGE);

这样,创建uploadf的活动将收到结果。所以你必须把你的onActivityResult移到你的Fragment
public class YourActivity extends Activity {
    ...
    public void onActivityResult(int requestCode, int resultCode, Intent data){}
    ...
}

在uploadf外部和父活动内部运行。
如果这不起作用,请在DialogFragment中搜索StartActivityForresult

09-30 14:26