MapFragment无法强制转换为android.support.v4.app.Fragment(HERE MAP API Mobile)

时间:2015-10-12 05:14:10

标签: java android android-fragments android-fragmentactivity here-api

我在MapFragment使用HERE MAP API for Mobile时遇到了一些错误。我将尝试在FragmentTabHost中添加MapFragment和我的MapFragment类我使用FragmentActivity类名是“EventMap”我的应用程序是这样的:我有一个ActionAarA活动是当一个菜单从这个应用程序将去FragmentTabHost。在FragmentTabHost我有两个tab frist标签是ListFragment用于显示我的数据,第二个是EventMap.class。现在当我运行应用程序并转到EventMap时我收到了错误请看这个错误日志:

10-12 11:48:20.681: E/AndroidRuntime(2894): FATAL EXCEPTION: main
10-12 11:48:20.681: E/AndroidRuntime(2894): Process: mpa.demo.bkklife, PID: 2894
10-12 11:48:20.681: E/AndroidRuntime(2894): java.lang.ClassCastException: mpa.demo.bkklife.EventMap cannot be cast to android.support.v4.app.Fragment
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.support.v4.app.Fragment.instantiate(Fragment.java:423)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:343)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.support.v4.app.FragmentTabHost.onTabChanged(FragmentTabHost.java:311)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:473)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.widget.TabHost.setCurrentTab(TabHost.java:453)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:176)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:646)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.view.View.performClick(View.java:5184)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.view.View$PerformClick.run(View.java:20910)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.os.Handler.handleCallback(Handler.java:739)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.os.Handler.dispatchMessage(Handler.java:95)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.os.Looper.loop(Looper.java:145)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at android.app.ActivityThread.main(ActivityThread.java:5942)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at java.lang.reflect.Method.invoke(Native Method)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at java.lang.reflect.Method.invoke(Method.java:372)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
10-12 11:48:20.681: E/AndroidRuntime(2894):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

好的,看看我的代码是 FragmentTabHost className:EventFragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class EventFragment extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        super.onDestroyView();
        mTabHost = null;
    }

    private FragmentTabHost mTabHost;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_event);

        mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("รายการแจ้งเหตุ"),
                EventListView.class, null);

        mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("แผนที่"),EventMap.class,null);

        return mTabHost;
    }



    public EventFragment() {
        // TODO Auto-generated constructor stub
    }

}

这是一个 EventMap.class

import com.here.android.mpa.common.GeoCoordinate;
import com.here.android.mpa.common.OnEngineInitListener;
import com.here.android.mpa.mapping.Map;
import com.here.android.mpa.mapping.MapFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class EventMap extends FragmentActivity {
    // map embedded in the map fragment
    private Map map = null;
    // map fragment embedded in this activity
    private MapFragment mapFragment = null;

    public EventMap() {
        // TODO Auto-generated constructor stub
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map);
        if(map == null || mapFragment == null){
            init();
        }
    }

    public void init(){
        mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment);
        mapFragment.init(new OnEngineInitListener() {
            @Override
            public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
                if (error == OnEngineInitListener.Error.NONE) {
                    // retrieve a reference of the map from the map fragment
                    map = mapFragment.getMap();
                    // Set the map center coordinate to the Vancouver region (no animation)
                    map.setCenter(new GeoCoordinate(49.196261, -123.004773, 0.0), Map.Animation.NONE);
                    // Set the map zoom level to the average between min and max (no animation)
                    map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);

                } else {
                    System.out.println("ERROR: Cannot initialize Map Fragment:"+error.toString());
                }
            }
        });
    }




}

我错过了这个错误谢谢。

2 个答案:

答案 0 :(得分:0)

您必须将所有片段类更改为Supported Fragment类。

Fragment类支持的所有片段。

否则你会得到类强制转换异常。只是检查你的代码...

您还必须检查导入声明..

答案 1 :(得分:0)

您使用此

 // Obtain the SupportMapFragment and get notified when the map is ready to be used.



    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

实现OnMapReadyCallback接口和重写方法

  @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
   // do somthing
 }