对话框

时间:2016-05-08 18:52:50

标签: java android android-fragments android-studio

通过单击MainActivity中片段(SettingsBackImageDialog)中的按钮打开DialogFragment(TrainerSettings)。在SettingsBackImageDialog中有一些按钮,其中一个用于拍摄照片并将其设置为片段中的Imageview(trainersettingsmainicon)。

我在SettingsBackImageDialog中打开TrainerSettings

    public void onViewCreated (View view, Bundle savedInstanceState) {    
            view.findViewById(R.id.trainersettingsbackgroundbtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showEditbackImageDialog();
                }
            });
        }

private void showEditbackImageDialog() {
        android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
        SettingsBackImageDialog settingsBackImageDialog = SettingsBackImageDialog.newInstance("Wybierz pierwszy kolor");
        settingsBackImageDialog.show(fm, "SettingsBackImageDialog");
    }

SettingsBackImageDialog的代码是:

    public class SettingsBackImageDialog extends DialogFragment {

        int REQUESTCODE=1;

        public SettingsBackImageDialog() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.settingsbackgroundimgdialog, container);
        }

        @Override
        public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            view.findViewById(R.id.takephotobtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i, REQUESTCODE);
                }
            });

            String title = getArguments().getString("txt", "txt");
            getDialog().setTitle(title);
            getDialog().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }


        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            Toast.makeText(getActivity(), "before", Toast.LENGTH_LONG).show();
            if(requestCode==REQUESTCODE & resultCode== Activity.RESULT_OK){
                Bundle bundle = new Bundle();
                bundle = data.getExtras();
                Bitmap bitmap = (Bitmap)bundle.get("data");
                ImageView img = (ImageView) getActivity().findViewById(R.id.trainersettingsmainicon);
                img.setImageBitmap(bitmap);
                Toast.makeText(getActivity(), "in", Toast.LENGTH_LONG).show();
            }
        }

    }

MainActivity

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.MainContainer);
    fragment.onActivityResult(requestCode, resultCode, data);
    Toast.makeText(getApplicationContext(), "im here", LENGTH_SHORT).show();
}

应用程序打开相机,我可以接受piture和"接受"但没有别的事情发生。可能应用程序从不使用onActivityResult方法(我尝试在此方法中编写一些消息,但没有任何事情发生)。

在AndroidMonitor中:

05-09 02:43:50.299 9709-10068/com.hgyghyfghyu.apkana40 W/GooglePlayServicesUtil: Google Play services out of date.  Requires 8115000 but found 5089070
05-09 02:43:50.339 9709-9709/com.hgyghyfghyu.apkana40 W/EGL_emulation: eglSurfaceAttrib not implemented

我使用nox app模拟Android手机。

我该怎么做才能解决我的问题?

1 个答案:

答案 0 :(得分:2)

所选答案here完全解决了您的问题。您还需要检查this answer,它解决了应该处理onActivityResult的问题。 我的建议是你改变你的代码如下:

@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);
   view.findViewById(R.id.takephotobtn).setOnClickListener(new  View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //changed getActivity().startActivityForResult(i, REQUESTCODE); to:
                startActivityForResult(i, REQUESTCODE);
            }
        });

        String title = getArguments().getString("txt", "txt");
        getDialog().setTitle(title);
        getDialog().getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }

onActivityResult()将首先在Activity上调用。之后,如果您在“活动”中致电Fragments,则会与super.onActivityResult()联系。因此,您需要在onActivityResult中添加Activity回调 - 以便可以调用Fragment中的回调。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
}

完成后,您的片段上的onActivityResult将被调用,您的代码将按预期执行。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==REQUESTCODE & resultCode== Activity.RESULT_OK){
        Bundle bundle = new Bundle();
        bundle = data.getExtras();
        Bitmap bitmap = (Bitmap)bundle.get("data");
        ImageView img = (ImageView) getActivity().findViewById(R.id.trainersettingsmainicon);
        img.setImageBitmap(bitmap);
    }
}

你需要看看这两个密切相关的