我正在构建一个看起来像这样的应用程序。
它有一个操作栏和选项卡,如下所示。
选项卡 1:应显示带有 ViewPage 和指示器的图像。
选项卡 2:应显示地图。
我是 android 新手,但仍然以某种方式设法使用互联网资源并在示例源的帮助下构建了一个应用程序。但我被困在 mapview 附近(点击 Tab2 时)。我使用了 TabHost,但是这很有效,后来才知道 google 已经发布了支持 Map 片段的新 API。我什至将 Google API 用于 MapFragment(新的),但我最终得到的是一些垃圾代码。我把一切都搞砸了。我想从头开始。任何人都可以简要介绍如何实施它。
MapFragment.java
public class MapsFragment extends Fragment {
MapView map;
LayoutInflater inflater_;
ViewGroup container_;
View layout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
inflater_=inflater;
container_=container;
super.onCreate(savedInstanceState);
layout = inflater.inflate(R.layout.fragment_maps, container, false);
map = (MapView) layout.findViewById(R.id.mapView);
return (LinearLayout) layout;
}
PlaceActivity.java
public class PlaceActivity extends SherlockFragmentActivity implements
OnShareTargetSelectedListener {
FragmentManager fm = getSupportFragmentManager();
DetailsFragment fragment_det = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabMaps = actionBar.newTab();
ActionBar.Tab tabDetails = actionBar.newTab();
tabDetails.setText("Details");
tabMaps.setText("Maps");
tabDetails.setTabListener(new MyTabListener());
tabMaps.setTabListener(new MyTabListener());
actionBar.addTab(tabDetails);
actionBar.addTab(tabMaps);
}
private class MyTabListener implements ActionBar.TabListener {
@Override
public void onTabSelected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
if (tab.getPosition() == 0) {
if (fragment_det == null) {
System.out.println("again");
fragment_det = new DetailsFragment();
}
ft.replace(android.R.id.content, fragment_det);
} else {
MapAsyc m = new MapAsyc();
ft.replace(android.R.id.content, m);
}
}
@Override
public void onTabUnselected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_place, menu);
MenuItem actionItem = menu.findItem(R.id.action_share);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem
.getActionProvider();
actionProvider.setShareHistoryFileName(null);
actionProvider.setOnShareTargetSelectedListener(this);
actionProvider.setShareIntent(createShareIntent());
return true;
}
public boolean onShareTargetSelected(ShareActionProvider source,
Intent intent) {
// TODO Auto-generated method stub
this.startActivity(createShareIntent());
return true;
}
private Intent createShareIntent() {
String shareText = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareText);
return sharingIntent;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
case R.id.action_share:
Toast.makeText(this, "Share Via", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
}
DetailsFragment.java
public class DetailsFragment extends SherlockFragment
{
ImageFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
//container.removeAllViews();
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_details, container,
false);
mAdapter = new ImageFragmentAdapter(getActivity().getSupportFragmentManager());
mPager = (ViewPager) view.findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
return view;
}
}
我的 Tab 1 工作正常。当我点击选项卡 2 时,地图将加载。但是当我再次单击 tab1 时,我收到以下错误:::。
12-06 02:30:13.891: E/AndroidRuntime(16904): java.lang.IllegalStateException: Recursive entry to executePendingTransactions.
一些帖子建议使用 ASYNC 任务。但我不能使用它,因为我应该将 MapFragment 中的视图返回给侦听器以采取行动。
完整的错误日志
12-06 22:54:58.609: E/AndroidRuntime(28745): FATAL EXCEPTION: main
12-06 22:54:58.609: E/AndroidRuntime(28745): java.lang.IllegalStateException: Recursive entry to executePendingTransactions
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.view.ViewPager.populate(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.view.ViewPager.setCurrentItemInternal(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.view.ViewPager.setCurrentItemInternal(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.view.ViewPager.onRestoreInstanceState(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.view.View.dispatchRestoreInstanceState(View.java:12088)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2582)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2588)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.view.View.restoreHierarchyState(View.java:12066)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.Fragment.restoreViewState(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.BackStackRecord.run(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl$1.run(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.os.Handler.handleCallback(Handler.java:725)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.os.Looper.loop(Looper.java:137)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-06 22:54:58.609: E/AndroidRuntime(28745): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 22:54:58.609: E/AndroidRuntime(28745): at java.lang.reflect.Method.invoke(Method.java:511)
12-06 22:54:58.609: E/AndroidRuntime(28745): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-06 22:54:58.609: E/AndroidRuntime(28745): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-06 22:54:58.609: E/AndroidRuntime(28745): at dalvik.system.NativeStart.main(Native Method)