gpt4 book ai didi

android - 在 ViewPager fragment 中使用 Google Map V2 崩溃?

转载 作者:搜寻专家 更新时间:2023-11-01 08:02:30 25 4
gpt4 key购买 nike

我在我的 Android 应用程序中尝试使用 Google map v2 时遇到问题。

我的计划是有一个包含两个页面的 viewpager(一个“详细信息”页面和一个“googlemap”页面)

我已经设置好了所有的东西,它给了我一些奇怪的结果:

当我开始滑动到“googlemap”页面时,在动画中间(就像我半按住 viewpager 一样),我可以看到 map 正确加载。它就在那里。

我设置的所有东西(标记、相机、动画...)都按预期运行。

但是我一完成滑动,googlemap 页面就占据了整个屏幕,整个事情就崩溃了。

知道是什么原因造成的吗?

这里是所有相关代码:

FragmentMap.xml

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

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="200dp"/>

</RelativeLayout>

MapFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MapFragment extends Fragment {

private GoogleMap map;
private static View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
if (view != null)
{
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
view = inflater.inflate(R.layout.fragment_map, container, false);
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

getActivity().getSupportFragmentManager().popBackStack();
return view;

}

public static MapFragment newInstance() {
MapFragment f = new MapFragment();
return f;
}
}

DetailActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class DetailActivity extends FragmentActivity {

int extraID;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_holder);

Intent prevIntent = getIntent();
extraID = prevIntent.getIntExtra("selected_ID", 0);

ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}

private class MyPagerAdapter extends FragmentPagerAdapter {

public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return DetailFragment.newInstance(extraID);
case 1:
return MapFragment.newInstance();
default:
return null;
}
}

@Override
public int getCount() {
return 2;
}
}
}

detailHolder.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

编辑:日志

09-28 13:41:42.830: E/AndroidRuntime(12693): FATAL EXCEPTION: main
09-28 13:41:42.830: E/AndroidRuntime(12693): java.lang.NullPointerException
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.view.Choreographer.doFrame(Choreographer.java:531)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.os.Handler.handleCallback(Handler.java:725)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.os.Looper.loop(Looper.java:137)
09-28 13:41:42.830: E/AndroidRuntime(12693): at android.app.ActivityThread.main(ActivityThread.java:5230)
09-28 13:41:42.830: E/AndroidRuntime(12693): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 13:41:42.830: E/AndroidRuntime(12693): at java.lang.reflect.Method.invoke(Method.java:525)
09-28 13:41:42.830: E/AndroidRuntime(12693): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
09-28 13:41:42.830: E/AndroidRuntime(12693): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
09-28 13:41:42.830: E/AndroidRuntime(12693): at dalvik.system.NativeStart.main(Native Method)

最佳答案

以下是我的看法:

  1. 将detailHolder.xml改为detailholder.xml android xml文件全部小写

  2. 此代码可能会导致 map 问题

    if (parent != null)
    parent.removeView(view);

您正在使用相对布局,并且您删除了与该布局关联的任何 View ,这将影响字段中的所有布局。您可以使用其他布局,例如线性布局。

您的代码不完整。您如何使用 DetailFragment。

关于android - 在 ViewPager fragment 中使用 Google Map V2 崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19066499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com