在为android中的DrawerLayout添加Relativelayout后,为什么我的应用程序强制关闭?

时间:2014-05-02 17:48:25

标签: android listview relativelayout drawerlayout

我正在尝试为滑块Listview获取阴影效果。我试图将一个RelativeLayout与阴影图像放在ListView下面。我已经在我的xml文件中完成了以下操作但是因为以下错误而关闭了force。请指导我一步一步做什么以及我哪里出错。

我的xml文件

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"

android:layout_height="match_parent">


<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 


   >




</FrameLayout>


     <FrameLayout
        android:layout_width="250dp"
        android:layout_height="match_parent"
         > 

   <ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="#656565"
    android:dividerHeight="0.01dp"
     android:fadingEdgeLength="10dp"

    android:background="@color/list_background"
    android:listSelector="@drawable/list_selector"
   />

</FrameLayout>
   </android.support.v4.widget.DrawerLayout>

错误日志:

 05-02 17:37:03.256: E/AndroidRuntime(3132): FATAL EXCEPTION: main
 05-02 17:37:03.256: E/AndroidRuntime(3132): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taskmanager/com.example.taskmanager.Profile}: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.os.Handler.dispatchMessage(Handler.java:99)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.os.Looper.loop(Looper.java:137)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.app.ActivityThread.main(ActivityThread.java:5041)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at java.lang.reflect.Method.invokeNative(Native Method)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at java.lang.reflect.Method.invoke(Method.java:511)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at dalvik.system.NativeStart.main(Native Method)
 05-02 17:37:03.256: E/AndroidRuntime(3132): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:857)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1058)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at com.example.taskmanager.Profile.SelectItem(Profile.java:311)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at com.example.taskmanager.Profile.onCreate(Profile.java:152)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.app.Activity.performCreate(Activity.java:5104)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
 05-02 17:37:03.256: E/AndroidRuntime(3132):    ... 11 more
 05-02 17:38:42.195: E/Trace(3353): error opening trace file: No such file or directory (2)
 05-02 17:38:44.306: E/AndroidRuntime(3353): FATAL EXCEPTION: main
 05-02 17:38:44.306: E/AndroidRuntime(3353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taskmanager/com.example.taskmanager.Profile}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.os.Handler.dispatchMessage(Handler.java:99)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.os.Looper.loop(Looper.java:137)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.app.ActivityThread.main(ActivityThread.java:5041)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at java.lang.reflect.Method.invokeNative(Native Method)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at java.lang.reflect.Method.invoke(Method.java:511)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at dalvik.system.NativeStart.main(Native Method)
 05-02 17:38:44.306: E/AndroidRuntime(3353): Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:857)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1058)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at com.example.taskmanager.Profile.SelectItem(Profile.java:311)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at com.example.taskmanager.Profile.onCreate(Profile.java:152)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.app.Activity.performCreate(Activity.java:5104)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
 05-02 17:38:44.306: E/AndroidRuntime(3353):    ... 11 more

Profile.class

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    final HashMap<String, String> c=db.getAddedContacts();
    // Initializing
    dataList = new ArrayList<DrawerItem>();
    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
     TypefaceSpan tp;
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);
          final ActionBar actionBar = getActionBar();

    actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.green));
    actionBar.setCustomView(R.layout.actionbars);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setIcon(
              new ColorDrawable(getResources().getColor(android.R.color.transparent)));  

1 个答案:

答案 0 :(得分:1)

显然你在FrameLayout里面有你的listView。如果你删除它,一切都会好起来的。

否则在xml

中将id设置为2nd frameLayout
android:id="@+id/framelayout"

然后获取FrameLayout

的实例
FrameLayout frameLayout = (LinearLayout) findViewById(R.id.frameLayout);

FrameLayout的实例传递给mDrawerLayout.closeDrawer(frameLayout);

希望这能解决问题。