我正在使用 View Pager 显示三个视图。
"Filter","Streets","Around"
每个视图都由片段定义。
我想要什么: 我想在我的 FilterFragment 中使用 Mapview。
我做了什么 :
我想在过滤器片段中使用 MapView 但无法成功运行。和获取日志分类错误:
android.view.InflateException: Binary XML file line #33: Error inflating class com.google.android.maps.MapView
我还在 Net 和 Stackoverflow 上搜索了有关此问题的信息,并找到了LINK和LINK,但由于我正在使用带有 ViewPager 的 Fragment,因此无法找到与我的问题相关的链接。
我把我的代码如下。
这是我的 MainViewPager.java 代码:
package com.example.viewpageractivitydemo;
import com.example.viewpageractivitydemo.viewpager.TitlePageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
public class MainViewPager extends FragmentActivity {
TestFragmentAdapter adapter;
ViewPager pager;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
Log.i("MainViewPager","Inside Oncreate");
setContentView(R.layout.mainviewpager);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new TestTitleFragmentAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
pager.setCurrentItem(1);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// pager.setCurrentItem(1);
}
}
这是 TestFragmentAdapter.java 的代码
package com.example.viewpageractivitydemo;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
public class TestFragmentAdapter extends FragmentPagerAdapter {
protected static final String[] CONTENT = new String[] { "Filter",
"Streets", "Around" };
public String TAG = "TestFragmentAdapter";
private int mCount = CONTENT.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
Log.i(TAG, "Inside getItemMethod with position " + position);
if (position == 0) {
Log.i("TestFragmentAdapter","Inside if Condition");
return FilterFragment.newInstance(position);
} else if (position == 1) {
return StreetsFragment.newInstance(position);
} else if (position == 2) {
return AroundFragment.newInstance(position);
} else {
return null;
}
// return MainFragment.newInstance(CONTENT[position % CONTENT.length]);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mCount;
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
这是我的 FilterFragment.java 代码:
package com.example.viewpageractivitydemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FilterFragment extends Fragment {
/*
* @Override protected Class<? extends Activity> getActivityClass() { //
* TODO Auto-generated method stub return MyFilterActivity.class; }
*/
public static Fragment newInstance(int position) {
Log.i("FilterFragment","Inside New Instasnce");
FilterFragment filterFragment = new FilterFragment();
return filterFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("FilterFragment","Inside OcCreateView");
View view = inflater.inflate(R.layout.filter, null);
return view;
}
}
这是我的 filter.xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#C6CDD4"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#93A4BA"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="5dp"
android:gravity="center"
android:text="Where Am I"
android:textColor="#ffffff"
android:textSize="18dp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/whereami_mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0-PaH6rk7RKrZSMd_lqdrZi6FazYyIlX5r6YFyA"
android:clickable="true" />
<!--
<com.google.android.maps.MapView
android:id="@+id/whereami_mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0-PaH6rk7RKrZSMd_lqdrZi6FazYyIlX5r6YFyA" />
-->
</LinearLayout>
</LinearLayout>
请任何人都可以建议我回答以解决我的问题。我在这上面花了很多时间。仍然对我的问题没有任何想法。提前谢谢。