gpt4 book ai didi

android - 将 SupportMapFragment 放在 DialogFragment 上

转载 作者:太空狗 更新时间:2023-10-29 15:46:10 26 4
gpt4 key购买 nike

我尝试在 DialogFragment 上添加 SupportMapFragment,但它返回类 fragment 膨胀错误。我不明白为什么它返回 error inflating class fragment。我认为我使用的方法是正确的。我需要从 DialogFragment 扩展,因为我想在对话框中显示 map 。

我已经关注了这个 answer , 和 this answer也是,没有人能解决我的问题。

有人可以帮助我吗?

谢谢。

这是我的 DialogFragment :

import android.support.v4.app.DialogFragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;

/* For widget */
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.Button;

/* Image Downloader Library */
import com.squareup.picasso.Picasso;

/* For string formater */
import com.yai.app.math.support.StringFormater;

/* For object */
import com.yai.app.object.support.Property;
import com.yai.app.object.support.User;

/* For context */
import android.content.Context;

import com.google.android.gms.maps.CameraUpdateFactory;

/* For map */
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class DialogFragmentReadAdvertisement extends DialogFragment{
private Property propertyData;
private User userData;
private Context contextData;

private GoogleMap mapDetail;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

propertyData = MainActivity.activeProperty;
userData = MainActivity.activeUser;
contextData = App.getContext();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.dialog_fragment_read_advertisement, container, false);

mapDetail = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_data))
.getMap();

mapDetail.addMarker(
new MarkerOptions().position(propertyData.getLocation())
);

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(propertyData.getLocation())
.zoom(19)
.tilt(0)
.bearing(0)
.build();

mapDetail.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

return view;
}
}

这是我的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<!-- One view for ScrollView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<!-- Line 13 bellow -->
<fragment
android:id="@+id/map_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>
</ScrollView>

这是我的 LogCat:

07-14 16:28:46.291: E/AndroidRuntime(28121): FATAL EXCEPTION: main
07-14 16:28:46.291: E/AndroidRuntime(28121): android.view.InflateException: Binary XML file line #13: Error inflating class fragment
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-14 16:28:46.291: E/AndroidRuntime(28121): at com.yai.properti.tujuh.tujuh.tujuh.DialogFragmentReadAdvertisement.onCreateView(DialogFragmentReadAdvertisement.java:55)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:310)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-14 16:28:46.291: E/AndroidRuntime(28121): at com.yai.properti.tujuh.tujuh.tujuh.DialogFragmentReadAdvertisement.onCreateView(DialogFragmentReadAdvertisement.java:55)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.os.Handler.handleCallback(Handler.java:615)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.os.Looper.loop(Looper.java:137)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.app.ActivityThread.main(ActivityThread.java:4895)
07-14 16:28:46.291: E/AndroidRuntime(28121): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 16:28:46.291: E/AndroidRuntime(28121): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 16:28:46.291: E/AndroidRuntime(28121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-14 16:28:46.291: E/AndroidRuntime(28121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-14 16:28:46.291: E/AndroidRuntime(28121): at dalvik.system.NativeStart.main(Native Method)
07-14 16:28:46.291: E/AndroidRuntime(28121): Caused by: java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f05009e, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
07-14 16:28:46.291: E/AndroidRuntime(28121): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
07-14 16:28:46.291: E/AndroidRuntime(28121): ... 30 more

最佳答案

我在 DialogFragment 中添加 GoogleMapFragment 时遇到问题,我已将其解决为

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (Build.VERSION.SDK_INT < 21) {
supportMapFragment = (SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.mapDialog);
} else {
supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.mapDialog);
}
supportMapFragment.getMapAsync(this);
}

Override onDestroy() as

 @Override
public void onDestroy() {
super.onDestroy();
if (null != supportMapFragment)
getActivity().getSupportFragmentManager().beginTransaction()
.remove(supportMapFragment)
.commit();
}

关于android - 将 SupportMapFragment 放在 DialogFragment 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24733436/

26 4 0