我有一个问题,我正在尝试在我的Android应用中使用Google Maps v2。
我的计划是有一个包含两个页面的viewpager(“详细”页面和“googlemap”页面)
我把整个事情搞定了,这给了我一些奇怪的结果:
当我开始滑动到“googlemap”页面时,在动画中间(就像我中途持有viewpager一样),我可以看到地图正确加载。它就在那里。
我设置的所有东西(标记,相机,动画......)按照预期运行。
但是很快我完成了滑动,并且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" />
编辑: 的 logcat的
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)
答案 0 :(得分:3)
以下是我的意见:
将detailHolder.xml更改为detailholder.xml android xml文件均为小写
此代码可能导致地图问题
if (parent != null)
parent.removeView(view);
您正在使用相对布局,并删除与布局关联的任何视图,它将影响字段中的所有布局。您可以使用其他布局,例如linearlayout。
您的代码不完整。你是如何使用DetailFragment的。