使用滑动菜单,视图无法转换为片段中的ViewGroup

时间:2016-08-13 07:10:02

标签: android android-fragments android-view slidingmenu android-viewgroup

此异常发生在

    slidingMenu.attachToActivity(this, SlidingMenu.LEFT);

当我尝试显示Mark点击的额外信息时。我想在滑动菜单中显示内容。 “this”对象指的是扩展AppCompatActivity类的MapActivity类(我的自定义java类)。 此方法位于My MapActivity类中,该类负责初始化滑动菜单:

private void initSlideMenu(){
    slidingMenu = new SlidingMenu(this);
    slidingMenu.setMode(SlidingMenu.RIGHT);
    double dimension = Master.checkDimension(G.currentActivity);
    Log.i("dimension", dimension + "");
    if (dimension < 4)
        slidingMenu.setBehindOffset(100);
    else if (dimension >= 4 && dimension <= 5)
        slidingMenu.setBehindOffset(300);
    else if (dimension > 5)
        slidingMenu.setBehindOffset(250);
    slidingMenu.setFadeDegree(0.8f);
    slidingMenu.setFadeEnabled(true);
    slidingMenu.attachToActivity(this, SlidingMenu.LEFT);
    if (dimension <= 6) {
        slidingMenu.setSlidingEnabled(false);
    }
    View view = G.inflater.inflate(R.layout.map_details, null);
    slidingMenu.setMenu(view);


}

和我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <fragment
        android:name="com.google.android.gms.maps.MapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


</LinearLayout>

我的Java课程:

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {

GoogleMap googleMap;
MapFragment mMapFragment;
LatLng myLocation;
public boolean firstTime = true;
ArrayList<Double> xList = new ArrayList<>();
ArrayList<Double> yList = new ArrayList<>();
SlidingMenu slidingMenu;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_activity);
    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);

    mapFragment.getMapAsync(this);

}

@Override
public void onMapReady(final GoogleMap map) {

    Log.i("Position", "SS");
    map.setMyLocationEnabled(true);
    final BitmapDescriptor saharsunLocationIcon = BitmapDescriptorFactory.fromResource(R.drawable.saharsun_smal);
    final BitmapDescriptor myLocationIcon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);
    // Check if we were successful in obtaining the map.
    map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            if (marker.getTitle().equals("I'm Hear")) {
                MediaPlayer mPlayer2;
                mPlayer2 = MediaPlayer.create(G.currentActivity, R.raw.cow_sound);
                mPlayer2.start();
                initSlideMenu();
                Log.i("MARKER", marker.getTitle());
            }
            Log.i("MARKER", marker.getTitle());

            return true;
        }
    });
    if (map != null) {

        map.setOnMyLocationChangeListener(new                      GoogleMap.OnMyLocationChangeListener() {

            @Override
            public void onMyLocationChange(Location location) {
                // TODO Auto-generated method stub
                Random random = new Random();
                myLocation = new LatLng(location.getLatitude(),
                        location.getLongitude());

                Log.i("Position", location.getLatitude() + " " + location.getLongitude());
                if (firstTime) {
                    map.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 16));
                    map.addMarker(new MarkerOptions().position(
                            new LatLng(location.getLatitude(), location.getLongitude())).title("It's Me!")).setIcon(myLocationIcon);

                    for (int i = 0; i < 5; i++) {
                        double x = random.nextDouble() / 100 - 0.001 + location.getLatitude();
                        double y = random.nextDouble() / 100 - 0.001 + location.getLongitude();
                        xList.add(x);
                        yList.add(y);
                        map.addMarker(new MarkerOptions().position(
                                new LatLng(x, y)).title("I'm Hear")).setIcon(saharsunLocationIcon);

                    }
                    firstTime = false;

                }
            }
        });

    }
}

@Override
protected void onResume() {
    super.onResume();
    G.currentActivity = this;
}

和日志:

08-13 11:44:53.479 19262-19262/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: mahdi.yusefi.qrcodereader, PID: 19262
java.lang.ClassCastException: android.view.View cannot be cast to android.view.ViewGroup
    at com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.attachToActivity(SlidingMenu.java:318)
    at com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.attachToActivity(SlidingMenu.java:292)
    at mahdi.yusefi.qrcodereader.MapActivity.initSlideMenu(MapActivity.java:124)
    at mahdi.yusefi.qrcodereader.MapActivity.access$000(MapActivity.java:26)
    at mahdi.yusefi.qrcodereader.MapActivity$1.onMarkerClick(MapActivity.java:62)
    at com.google.android.gms.maps.GoogleMap$10.a(Unknown Source)
    at com.google.android.gms.maps.internal.n$a.onTransact(Unknown Source)
    at android.os.Binder.transact(Binder.java:380)
    at yc.a(:com.google.android.gms.DynamiteModulesB:84)
    at maps.ad.T.b(Unknown Source)
    at maps.D.d.a(Unknown Source)
    at maps.V.k.a(Unknown Source)
    at maps.V.u.d(Unknown Source)
    at maps.V.P.onSingleTapConfirmed(Unknown Source)
    at maps.z.e$b.onSingleTapConfirmed(Unknown Source)
    at maps.z.c$a.handleMessage(Unknown Source)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5257)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:921)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:716)

1 个答案:

答案 0 :(得分:0)

意外地找到了解决方案。我应该在onCreate方法中初始化我的幻灯片菜单。每次我想在Mark上显示它我只需要调用

slidingMenu.toggle();

但仍然没有解释为什么会发生此异常。