Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试在外部活动(片段)中返回PlacePicker信息时未调用onActivityResult_Android_Performance_Android Fragments - Fatal编程技术网

Android 尝试在外部活动(片段)中返回PlacePicker信息时未调用onActivityResult

Android 尝试在外部活动(片段)中返回PlacePicker信息时未调用onActivityResult,android,performance,android-fragments,Android,Performance,Android Fragments,我正在做一个自定的项目,我正在努力使它成为一个可以从某个片段调用分发器类的片段,在这个片段中我创建了PlacePicker方法。PlacePicker可以很好地打开所有内容,但当我关闭它时,它不会返回我选择的位置的任何信息。我一直试图通过查看互联网上十多篇帖子(包括StackOverflow)来了解这一点,但我还没有找到适合我的解决方案 这是存储片段的占位符片段类 import android.os.Bundle; import android.support.v4.app.Fragment;

我正在做一个自定的项目,我正在努力使它成为一个可以从某个片段调用分发器类的片段,在这个片段中我创建了PlacePicker方法。PlacePicker可以很好地打开所有内容,但当我关闭它时,它不会返回我选择的位置的任何信息。我一直试图通过查看互联网上十多篇帖子(包括StackOverflow)来了解这一点,但我还没有找到适合我的解决方案

这是存储片段的占位符片段类

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


public class PlaceholderFragment extends Fragment {


    private static final String TAG = "PlaceholderFragment";

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = null;
        if (getArguments().getInt(ARG_SECTION_NUMBER)==1){
            rootView = inflater.inflate(R.layout.fragment_createevent_page1, container, false);
            EditText eventName =(EditText) rootView.findViewById(R.id.editEventName);
            EditText eventOrganiser =(EditText) rootView.findViewById(R.id.editOrganiser);
            EditText eventDescription =(EditText) rootView.findViewById(R.id.editTextDescriptionOfEvent);

        }
        else if(getArguments().getInt(ARG_SECTION_NUMBER)==2){
            rootView = inflater.inflate(R.layout.fragment_createevent_page2, container, false);
            TextView createEventFirstDay = (TextView) rootView.findViewById(R.id.textViewFirstDay);
            TextView createEventLastDay = (TextView) rootView.findViewById(R.id.textViewLastDay);
            TextView createEventLocation = (TextView) rootView.findViewById(R.id.textViewLocation);
            TextView createEventImageView = (TextView) rootView.findViewById(R.id.textViewEventImage);
            ImageView eventImageView =(ImageView) rootView.findViewById(R.id.imageViewEventImage);

            createEventFirstDay.setOnClickListener(new MyDatePickerDialog(getContext(),createEventFirstDay,"First day: "));
            createEventLastDay.setOnClickListener(new MyDatePickerDialog(getContext(),createEventLastDay, "Last day: "));
            createEventLocation.setOnClickListener(new Dispenser(getActivity(),createEventLocation,getContext(),101));
            createEventImageView.setOnClickListener(new Dispenser(getActivity(), createEventImageView,eventImageView,102));

            //textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));

        }

        return rootView;
    }

}
这是分发器类

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.ui.PlacePicker;

public class Dispenser implements View.OnClickListener{

    private static final String TAG = "Dispenser";

    private Activity localActivity;
    private Context localContext;
    private TextView localTextView;
    private ImageView localImageView;

    private int local_requested_code;

    private static final int PLACE_PICKER_REQUEST = 101;
    private static final int CHOOSE_IMAGE_REQUEST = 102;

    public Dispenser(Activity activity, TextView textView, ImageView imageView, int requested_code){   //Dispenser for finding an image from the phone storage
        this.localActivity = activity;
        this.localTextView = textView;
        this.localImageView = imageView;
        this.local_requested_code = requested_code;
    }
    public Dispenser(Activity activity, TextView textView, Context context, int requested_code){    //Dispenser for opening maps and finding required location
        this.localActivity = activity;
        this.localTextView = textView;
        this.localContext = context;
        this.local_requested_code = requested_code;
    }


    @Override
    public void onClick(View view) {
        if(local_requested_code==PLACE_PICKER_REQUEST){ //This is where we start the location process..
            PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
            Intent intent;
            try {
                intent = builder.build(localActivity);
                intent.setFlags(0);
                localActivity.startActivityForResult(intent, PLACE_PICKER_REQUEST);


            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }
        }
        else if(local_requested_code==CHOOSE_IMAGE_REQUEST){ //This is where we start the image finding process..
            Toast.makeText(localActivity, "Choosing an image from storage", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            localActivity.startActivityForResult(Intent.createChooser(intent,"Select Event Image"),CHOOSE_IMAGE_REQUEST);
        }
    }
protected  void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode==PLACE_PICKER_REQUEST){
        if(resultCode==localActivity.RESULT_OK){
            Place place = PlacePicker.getPlace(localContext,data);
            String locationAddress = String.format("Location address: %s",place.getAddress());
            String locationName = String.format("Location name: %s",place.getName());
            localTextView.setText(locationName+" | "+locationAddress);
        }
    }
    if(requestCode == CHOOSE_IMAGE_REQUEST && resultCode == localActivity.RESULT_OK && data != null && data.getData() != null){
        Uri uriEventImage = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(localActivity.getContentResolver(),uriEventImage);
            localImageView.setImageBitmap(bitmap);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

    private void uploadImageToFirebaseStorage() {
        // StorageReference eventImageReference = FirebaseStorage.getInstance().getReference("events/"+eventName+"_"+eventOrganiser+"_");
    }


}
感谢所有建议,提前感谢大家的帮助

您需要将onActivityResult方法移动到activity类,在本例中,您可以从该类调用startActivityForResult,即localActivity变量的类