【问题标题】:Open Close Maps with button (change layouts)使用按钮打开关闭地图(更改布局)
【发布时间】:2016-12-17 19:51:02
【问题描述】:

我正在尝试使用按钮打开和关闭地图(更改布局)。

我在MainActivity 中有一个按钮,它打开SecondActivity,这设置maps_layout 正确。

我在地图上有一个按钮。这会正确切换到另一个布局。

当我想切换回来时,我得到Fatal Exception

我的代码:

public class Game extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapy();
    }

    public void mapy() {
        setContentView(R.layout.mapy_test);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map_test);
        mapFragment.getMapAsync(this);

        Button changetocam = (Button) findViewById(R.id.changetocam);

        changetocam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cam();
            }
        });
    }

    public void cam() {
        setContentView(R.layout.ingame_masteroverlay_layout);

        Button changetomap = (Button) findViewById(R.id.changetomap);

        changetomap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mapy();
            }
        });
    }
}

这是错误日志:

`FATAL EXCEPTION: main Process: auftour.mrorhan.probieren, PID: 2689 android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0f010b, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment` 

我认为错误是因为我没有关闭mapy_test 布局,所以当我再次尝试连接到地图时,我有双重身份。

【问题讨论】:

  • 你能发布你的 logcat 错误吗?
  • 致命异常:主进程:auftour.mrorhan.probieren,PID:2689 android.view.InflateException:二进制 XML 文件第 6 行:二进制 XML 文件第 6 行:膨胀类片段时出错原因: android.view.InflateException: Binary XML file line #6: Error inflating class fragment 原因:java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0f010b, tag null, or parent id 0xffffffff with another fragment for com. google.android.gms.maps.SupportMapFragment
  • 顺便说一句,我认为错误来了,因为我没有关闭“mapy_test”布局,所以当我尝试再次连接到地图时,我有双 ID?
  • 我编辑了问题,将错误日志从评论中移到主要问题中。
  • ty 进行编辑 :)

标签: java android google-maps button maps


【解决方案1】:

// 试试这个它对我有用 查看 v;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (v != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(v);
    }
    try
 {
        v = inflater.inflate(R.layout.mapy_test, container, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }
    return v;
}

【讨论】:

  • 你的答案,我应该把它放在我从地图更改为其他布局的地方吗?
  • 你在使用 Fragments 吗?我不使用片段
  • 好的,我也找到了相同的答案 :) 但谢谢:p stackoverflow.com/questions/14083950/…
  • 我没用过。我只使用了片段和片段容器。我说只是为了你的努力。
【解决方案2】:

所以伙计们,我解决了我的问题。我使用框架和额外的框架容器布局。如果有人有同样的问题,我可以编写自己的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    相关资源
    最近更新 更多