You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fragment中调用外部类PlacePicker后onActivityResult未触发问题

解决Fragment中调用PlacePicker无法返回选中地点的问题

看起来你遇到的核心问题是PlacePicker的结果没有正确传递到Fragment中,这是因为你用了Activity的startActivityForResult来启动PlacePicker,导致结果返回给了Activity而不是你的PlaceholderFragment。下面是具体的诊断和修复方案:

问题根源

  1. 你在Dispenser类中调用localActivity.startActivityForResult(...),这会让结果回调到Activity的onActivityResult,而不是Fragment的。Fragment有自己的startActivityForResult方法,必须用它来启动,才能让结果正确路由到Fragment。
  2. 你的PlaceholderFragment没有重写onActivityResult方法,无法接收和处理PlacePicker返回的地点数据。
  3. 你的Dispenser类代码中catch块不完整,需要补全异常捕获逻辑。

修复步骤

1. 修改Dispenser类,支持Fragment启动PlacePicker

更新Dispenser的构造方法和点击逻辑,让它可以接收Fragment引用,并使用Fragment的startActivityForResult:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
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 Fragment localFragment;
    private Activity localActivity;
    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;

    // 新增:用于PlacePicker的构造方法(接收Fragment)
    public Dispenser(Fragment fragment, TextView textView, int requested_code){
        this.localFragment = fragment;
        this.localTextView = textView;
        this.local_requested_code = requested_code;
    }

    // 保留原有的图片选择构造方法
    public Dispenser(Activity activity, TextView textView, ImageView imageView, int requested_code){
        this.localActivity = activity;
        this.localTextView = textView;
        this.localImageView = imageView;
        this.local_requested_code = requested_code;
    }

    @Override
    public void onClick(View view) {
        if(local_requested_code == PLACE_PICKER_REQUEST){
            PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
            try {
                Intent intent = builder.build(localFragment.getActivity());
                // 使用Fragment的startActivityForResult,确保结果回到Fragment
                localFragment.startActivityForResult(intent, PLACE_PICKER_REQUEST);
            } catch (GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) {
                e.printStackTrace();
                Toast.makeText(localFragment.getContext(), "Google Play Services不可用,请检查", Toast.LENGTH_SHORT).show();
            }
        } else if(local_requested_code == CHOOSE_IMAGE_REQUEST){
            // 这里保留你原有的图片选择逻辑
        }
    }
}

2. 更新PlaceholderFragment的点击事件绑定

在PlaceholderFragment中,使用新的Dispenser构造方法来绑定地点选择的点击事件:

// 在section==2的分支里
createEventLocation.setOnClickListener(new Dispenser(this, createEventLocation, 101));

3. 在PlaceholderFragment中重写onActivityResult处理返回结果

添加onActivityResult方法,接收并解析PlacePicker返回的地点信息:

import android.app.Activity;
import android.content.Intent;
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;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;

public class PlaceholderFragment extends Fragment {
    // 保留你原有的代码...

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 处理PlacePicker的返回结果
        if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
            Place selectedPlace = PlacePicker.getPlace(getContext(), data);
            // 获取地点名称和地址
            String placeName = selectedPlace.getName().toString();
            String placeAddress = selectedPlace.getAddress().toString();
            // 更新TextView显示
            TextView createEventLocation = getView().findViewById(R.id.textViewLocation);
            createEventLocation.setText(String.format("%s\n%s", placeName, placeAddress));
        }
    }
}

4. 确保Activity的onActivityResult调用super

如果你的宿主Activity重写了onActivityResult方法,一定要加上super.onActivityResult(...),否则Fragment的onActivityResult不会被触发:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // 其他Activity级别的逻辑
}

这样修改后,PlacePicker关闭时,选中的地点信息就会正确传递到你的PlaceholderFragment中,并更新到对应的TextView上了。

内容的提问来源于stack exchange,提问作者LeoLyo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:36:49