我在我的 Android 应用程序中尝试使用 Google Maps v2 时遇到问题。
我的计划是有一个有两个页面的viewpager(一个“详细”页面和一个“googlemap”页面)
我已经设置了整个事情,它给了我一些奇怪的结果:
当我开始在动画中间滑动到“googlemap”页面时(就像我将viewpager 放在一半),我可以看到地图正确加载。它就在那里。
我设置的所有东西(标记、相机、动画......)都按预期运行。
但是,一旦我完成滑动,googlemap 页面就占据了整个屏幕,整个事情就崩溃了。
知道是什么原因造成的吗?
以下是所有相关代码:
片段映射.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)