ActionBarSherlock与谷歌地图和片段

时间:2013-04-10 22:45:37

标签: android android-fragments actionbarsherlock android-mapview

我正在尝试编译此example

我正在使用:

机器人支撑-V4-Google地图

ActionBarSherlock-Plugin-Maps v.4.0

我已经回复了这个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xrigau.mapsfragments/com.xrigau.mapsfragments.MapswithfragmentsActivity}: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.

在这一行:

Exchanger.mMapView = new MapView(this, "INSERT_YOUR_MAP_API_KEY_HERE");

你能帮我解决这个问题吗?

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // We instantiate the MapView here, it's really important!
        Exchanger.mMapView = new MapView(this, "INSERT_YOUR_MAP_API_KEY_HERE"); // TODO: Replace for API Key!

        setupFragments();
        // We manually show the list Fragment.
        showFragment(mMyListFragment);
    }

这是整个文件.java ...这个类扩展了SherlockFragmentActivity:

    package com.xrigau.mapsfragments;

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

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.maps.MapView;

public class MapswithfragmentsActivity extends SherlockFragmentActivity {

    private MapFragment mMapFragment;
    private MyListFragment mMyListFragment;

    // We use this fragment as a pointer to the visible one, so we can hide it easily.
    private Fragment mVisible = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // We instantiate the MapView here, it's really important!
        Exchanger.mMapView = new MapView(this, "INSERT_YOUR_MAP_API_KEY_HERE"); // TODO: Replace for API Key!

        setupFragments();
        // We manually show the list Fragment.
        showFragment(mMyListFragment);
    }

    /**
     * This method does the setting up of the Fragments. It basically checks if
     * the fragments exist and if they do, we'll hide them. If the fragments
     * don't exist, we create them, add them to the FragmentManager and hide
     * them.
     */
    private void setupFragments() {
        final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        /*
         * If the activity is killed while in BG, it's possible that the
         * fragment still remains in the FragmentManager, so, we don't need to
         * add it again.
         */
        mMapFragment = (MapFragment) getSupportFragmentManager().findFragmentByTag(MapFragment.TAG);
        if (mMapFragment == null) {
            mMapFragment = new MapFragment();
            ft.add(R.id.fragment_container, mMapFragment, MapFragment.TAG);
        }
        ft.hide(mMapFragment);

        mMyListFragment = (MyListFragment) getSupportFragmentManager().findFragmentByTag(MyListFragment.TAG);
        if (mMyListFragment == null) {
            mMyListFragment = new MyListFragment();
            ft.add(R.id.fragment_container, mMyListFragment, MyListFragment.TAG);
        }
        ft.hide(mMyListFragment);

        ft.commit();
    }

    /**
     * This method shows the given Fragment and if there was another visible
     * fragment, it gets hidden. We can just do this because we know that both
     * the mMyListFragment and the mMapFragment were added in the Activity's
     * onCreate, so we just create the fragments once at first and not every
     * time. This will avoid facing some problems with the MapView.
     * 
     * @param fragmentIn
     *            The fragment to show.
     */
    private void showFragment(Fragment fragmentIn) {
        if (fragmentIn == null) return;

        final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);

        if (mVisible != null) ft.hide(mVisible);

        ft.show(fragmentIn).commit();
        mVisible = fragmentIn;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu with the options to show the Map and the List.
        getSupportMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.ic_list:
            // Show mMyListFragment.
            showFragment(mMyListFragment);
            return true;

        case R.id.ic_map:
            // Show mMapFragment.
            showFragment(mMapFragment);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * This class acts as an exchanger between the Activity and the MapFragment,
     * so if you want, you can put the MapFragment class in a separate java
     * file.
     * 
     * @author Xavi
     * 
     */
    public static class Exchanger {
        // We will use this MapView always.
        public static MapView mMapView;
    }

    /**
     * This is our ListFragment class. You can put it in a separate java file.
     * 
     * @author Xavi
     * 
     */
    public static class MyListFragment extends SherlockListFragment {

        public static final String TAG = "listFragment";

        private final String[] mItems = { "List Item 1", "List Item 2",
                "List Item 3", "List Item 4", "List Item 5", "List Item 6",
                "List Item 7", "List Item 8", "List Item 9", "List Item 10" };

        public MyListFragment() {}

        @Override
        public void onCreate(Bundle arg0) {
            super.onCreate(arg0);
            setRetainInstance(true);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
            // Inflate the ListView layout file.
            return inflater.inflate(R.layout.list_fragment, null);
        }

        @Override
        public void onViewCreated(View arg0, Bundle arg1) {
            super.onViewCreated(arg0, arg1);
            setListAdapter(new ArrayAdapter<String>(getSherlockActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, mItems));
        }
    }

    /**
     * This is the Fragment class that will hold the MapView as its content
     * view. You can put it in a separate java file.
     * 
     * @author Xavi
     * 
     */
    public static class MapFragment extends SherlockFragment {

        public static final String TAG = "mapFragment";

        public MapFragment() {}

        @Override
        public void onCreate(Bundle arg0) {
            super.onCreate(arg0);
            setRetainInstance(true);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
            // The Activity created the MapView for us, so we can do some init stuff.
            Exchanger.mMapView.setClickable(true);
            Exchanger.mMapView.setBuiltInZoomControls(true); // If you want.

            /*
             * If you're getting Exceptions saying that the MapView already has
             * a parent, uncomment the next lines of code, but I think that it
             * won't be necessary. In other cases it was, but in this case I
             * don't this should happen.
             */
            /*
             * final ViewGroup parent = (ViewGroup) Exchanger.mMapView.getParent();
             * if (parent != null) parent.removeView(Exchanger.mMapView);
             */

            return Exchanger.mMapView;
        }
    }
}

SherlockMapActivity.java:

      package com.actionbarsherlock.app;

    import android.content.res.Configuration;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.Window;
    import android.view.ViewGroup.LayoutParams;
    import com.actionbarsherlock.ActionBarSherlock;
    import com.actionbarsherlock.ActionBarSherlock.OnActionModeFinishedListener;
    import com.actionbarsherlock.ActionBarSherlock.OnActionModeStartedListener;
    import com.actionbarsherlock.ActionBarSherlock.OnCreatePanelMenuListener;
    import com.actionbarsherlock.ActionBarSherlock.OnMenuItemSelectedListener;
    import com.actionbarsherlock.ActionBarSherlock.OnPreparePanelListener;
    import com.actionbarsherlock.app.ActionBar;
    import com.actionbarsherlock.view.ActionMode;
    import com.actionbarsherlock.view.Menu;
    import com.actionbarsherlock.view.MenuInflater;
    import com.actionbarsherlock.view.MenuItem;
    import com.google.android.maps.MapActivity;

    public abstract class SherlockMapActivity extends MapActivity implements OnCreatePanelMenuListener, OnPreparePanelListener, OnMenuItemSelectedListener, OnActionModeStartedListener, OnActionModeFinishedListener {
        private ActionBarSherlock mSherlock;

        protected final ActionBarSherlock getSherlock() {
            if (mSherlock == null) {
                mSherlock = ActionBarSherlock.wrap(this, ActionBarSherlock.FLAG_DELEGATE);
            }
            return mSherlock;
        }


        ///////////////////////////////////////////////////////////////////////////
        // Action bar and mode
        ///////////////////////////////////////////////////////////////////////////

        public ActionBar getSupportActionBar() {
            return getSherlock().getActionBar();
        }

        public ActionMode startActionMode(ActionMode.Callback callback) {
            return getSherlock().startActionMode(callback);
        }

        @Override
        public void onActionModeStarted(ActionMode mode) {}

        @Override
        public void onActionModeFinished(ActionMode mode) {}


        ///////////////////////////////////////////////////////////////////////////
        // General lifecycle/callback dispatching
        ///////////////////////////////////////////////////////////////////////////

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            getSherlock().dispatchConfigurationChanged(newConfig);
        }

        @Override
        protected void onPostResume() {
            super.onPostResume();
            getSherlock().dispatchPostResume();
        }

        @Override
        protected void onPause() {
            getSherlock().dispatchPause();
            super.onPause();
        }

        @Override
        protected void onStop() {
            getSherlock().dispatchStop();
            super.onStop();
        }

        @Override
        protected void onDestroy() {
            getSherlock().dispatchDestroy();
            super.onDestroy();
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            getSherlock().dispatchPostCreate(savedInstanceState);
            super.onPostCreate(savedInstanceState);
        }

        @Override
        protected void onTitleChanged(CharSequence title, int color) {
            getSherlock().dispatchTitleChanged(title, color);
            super.onTitleChanged(title, color);
        }

        @Override
        public final boolean onMenuOpened(int featureId, android.view.Menu menu) {
            if (getSherlock().dispatchMenuOpened(featureId, menu)) {
                return true;
            }
            return super.onMenuOpened(featureId, menu);
        }

        @Override
        public void onPanelClosed(int featureId, android.view.Menu menu) {
            getSherlock().dispatchPanelClosed(featureId, menu);
            super.onPanelClosed(featureId, menu);
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (getSherlock().dispatchKeyEvent(event)) {
                return true;
            }
            return super.dispatchKeyEvent(event);
        }

        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            getSherlock().dispatchSaveInstanceState(outState);
        }

        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            getSherlock().dispatchRestoreInstanceState(savedInstanceState);
        }

        ///////////////////////////////////////////////////////////////////////////
        // Native menu handling
        ///////////////////////////////////////////////////////////////////////////

        public MenuInflater getSupportMenuInflater() {
            return getSherlock().getMenuInflater();
        }

        @Override
        public void invalidateOptionsMenu() {
            getSherlock().dispatchInvalidateOptionsMenu();
        }

        public void supportInvalidateOptionsMenu() {
            invalidateOptionsMenu();
        }

        @Override
        public final boolean onCreateOptionsMenu(android.view.Menu menu) {
            return getSherlock().dispatchCreateOptionsMenu(menu);
        }

        @Override
        public final boolean onPrepareOptionsMenu(android.view.Menu menu) {
            return getSherlock().dispatchPrepareOptionsMenu(menu);
        }

        @Override
        public final boolean onOptionsItemSelected(android.view.MenuItem item) {
            return getSherlock().dispatchOptionsItemSelected(item);
        }

        @Override
        public void openOptionsMenu() {
            if (!getSherlock().dispatchOpenOptionsMenu()) {
                super.openOptionsMenu();
            }
        }

        @Override
        public void closeOptionsMenu() {
            if (!getSherlock().dispatchCloseOptionsMenu()) {
                super.closeOptionsMenu();
            }
        }


        ///////////////////////////////////////////////////////////////////////////


  // Sherlock menu handling
    ///////////////////////////////////////////////////////////////////////////

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        if (featureId == Window.FEATURE_OPTIONS_PANEL) {
            return onCreateOptionsMenu(menu);
        }
        return false;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onPreparePanel(int featureId, View view, Menu menu) {
        if (featureId == Window.FEATURE_OPTIONS_PANEL) {
            return onPrepareOptionsMenu(menu);
        }
        return false;
    }

    public boolean onPrepareOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if (featureId == Window.FEATURE_OPTIONS_PANEL) {
            return onOptionsItemSelected(item);
        }
        return false;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        return false;
    }


    ///////////////////////////////////////////////////////////////////////////
    // Content
    ///////////////////////////////////////////////////////////////////////////

    @Override
    public void addContentView(View view, LayoutParams params) {
        getSherlock().addContentView(view, params);
    }

    @Override
    public void setContentView(int layoutResId) {
        getSherlock().setContentView(layoutResId);
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        getSherlock().setContentView(view, params);
    }

    @Override
    public void setContentView(View view) {
        getSherlock().setContentView(view);
    }

    public void requestWindowFeature(long featureId) {
        getSherlock().requestFeature((int)featureId);
    }


    ///////////////////////////////////////////////////////////////////////////
    // Progress Indication
    ///////////////////////////////////////////////////////////////////////////

    public void setSupportProgress(int progress) {
        getSherlock().setProgress(progress);
    }

    public void setSupportProgressBarIndeterminate(boolean indeterminate) {
        getSherlock().setProgressBarIndeterminate(indeterminate);
    }

    public void setSupportProgressBarIndeterminateVisibility(boolean visible) {
        getSherlock().setProgressBarIndeterminateVisibility(visible);
    }

    public void setSupportProgressBarVisibility(boolean visible) {
        getSherlock().setProgressBarVisibility(visible);
    }

    public void setSupportSecondaryProgress(int secondaryProgress) {
        getSherlock().setSecondaryProgress(secondaryProgress);
    }
}

1 个答案:

答案 0 :(得分:0)

从例外情况来看,您已经扩展了Activity而不是MapActivity。

更改

public class MyMapActivity extends Activity {

public class MyMapActivity extends MapActivity {

您也可以考虑添加

<uses-library android:name="com.google.android.maps" />
清单文件中的

,如果你还没有。