无法在Fragment中使用toast工作

时间:2017-12-04 23:49:56

标签: android android-fragments google-places-api toast android-toast

我无法在片段中显示祝词。我正在使用PlacePicker,用户选择一个地方,吐司应该显示他们选择的地方。我试过了getContext()getActivity().getApplicationContext()& getActivity()但没有一个正在运作。

PlacePicker加载正常,它不会显示吐司。我还想显示用户在TextView中选择的位置,但是toast是主要优先级。

我的代码如下:

public class Tab1 extends Fragment {

int PLACE_PICKER_REQUEST = 1;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab1, container, false);
    return rootView;
}

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

    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
    try {
        startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
    } catch (GooglePlayServicesRepairableException e) {
        e.printStackTrace();
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }

    if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {
            Place place = PlacePicker.getPlace(data, getActivity());
            String toastMsg = String.format("Place: %s", place.getName());
            Toast.makeText(getContext(), toastMsg, Toast.LENGTH_LONG).show();
        }
    }
}
}

0 个答案:

没有答案