本文介绍了修复java.lang.ClassCastException在Android的抽屉式导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的,由抽屉式导航的机器人工作室的Andr​​oid应用程序。我收到称为错误的java.lang.ClassCastException:matrialdesign.practice.com.materialdesignpractice.MaterialDesign@41cdeb78必须实现OnFragmentInteractionListener
            在matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer.onAttach(navigatoin_drawer.java:90)\"

请帮我解决它,我是新来的Andr​​oid导航抽屉里,这是我的主要活动

 包matrialdesign.practice.com.materialdesignpractice;进口android.content.Intent;
进口android.support.v4.widget.DrawerLayout;
进口android.support.v7.app.ActionBarActivity;
进口android.os.Bundle;
进口android.support.v7.widget.Toolbar;
进口android.view.Menu;
进口android.view.MenuItem;
公共类MaterialDesign扩展ActionBarActivity
{
    工具栏tool_bar_widget;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.layout_material_design);
        tool_bar_widget =(工具栏)findViewById(R.id.toolbar);
        setSupportActionBar(tool_bar_widget);
        getSupportActionBar()setDisplayShowHomeEnabled(真)。
        。navigatoin_drawer导航=(navigatoin_drawer)getFragmentManager()findFragmentById(R.id.fragment);
        navigation.setup((DrawerLayout)findViewById(R.id.widget_drawer_layout),tool_bar_widget);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.menu_material_design,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_settings){
            返回true;
        }
        如果(ID == R.id.custom_nav_icon)
        {
            意图导航=新意图(MaterialDesign.this,Navigationactivity.class);
            startActivity(导航);
        }        返回super.onOptionsItemSelected(项目);
    }
}

这是我的导航抽屉活动:

 包matrialdesign.practice.com.materialdesignpractice;进口android.app.Activity;
进口android.net.Uri;
进口android.os.Bundle;
进口android.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.widget.DrawerLayout;
进口android.support.v7.app.ActionBarDrawerToggle;
进口android.support.v7.widget.Toolbar;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
/ **
 *一个简单的{@link片段}子类。
 *包含此片段必须实现活动
 * {@link navigatoin_drawer.OnFragmentInteractionListener}接口
 *处理交互事件。
 *使用{@link navigatoin_drawer#}的newInstance工厂方法
 *创建该片段的一个实例。
 * /
公共类navigatoin_drawer扩展片段
{
    // TODO:重命名参数的参数,选择匹配的名字
    //片段初始化参数,例如ARG_ITEM_NUMBER
    私人ActionBarDrawerToggle mDrawer_toogle;
    公共DrawerLayout mDrawer_layout;
    私有静态最后弦乐ARG_PARAM1 =参数1;
    私有静态最后弦乐ARG_PARAM2 =参数2;    // TODO:重命名和更改类型的参数
    私人字符串mParam1;
    私人字符串mParam2;    私人OnFragmentInteractionListener mListener;    / **
     *使用此工厂方法来创建新实例
     *使用提供的参数,这个片段。
     *
     * @参数参数1参数1。
     * @参数参数2参数2。
     返回:片段navigatoin_drawer的一个新实例。
     * /
    // TODO:重命名和更改类型和参数个数
    公共静态的newInstance navigatoin_drawer(字符串参数1,字符串参数2){
        navigatoin_drawer片段=新navigatoin_drawer();
        捆绑ARGS =新包();
        args.putString(ARG_PARAM1,参数1);
        args.putString(ARG_PARAM2,参数2);
        fragment.setArguments(参数);
        返回片段;
    }    公共navigatoin_drawer(){
        //必需的空公共构造
    }    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        如果(getArguments()!= NULL){
            mParam1 = getArguments()的getString(ARG_PARAM1)。
            mParam2 = getArguments()的getString(ARG_PARAM2)。
        }
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                             捆绑savedInstanceState){
        //充气的布局该片段
        返回inflater.inflate(R.layout.fragment_navigatoin_drawer,集装箱,FALSE);
    }    // TODO:重命名方法,更新参数和勾法将UI事件
    公共无效onButton pressed(URI URI){
        如果(mListener!= NULL){
            mListener.onFragmentInteraction(URI);
        }
    }    @覆盖
    公共无效onAttach(活动活动){
        super.onAttach(活动);
        尝试{
            mListener =(OnFragmentInteractionListener)活性;
        }赶上(抛出ClassCastException E){
            抛出新ClassCastException异常(activity.toString()
                    +必须实现OnFragmentInteractionListener);
        }
    }    @覆盖
    公共无效onDetach(){
        super.onDetach();
        mListener = NULL;
    }    公共无效设置(DrawerLayout drawerlayout,工具栏tool_bar)
    {
        mDrawer_layout = drawerlayout;
        mDrawer_toogle =新ActionBarDrawerToggle(getActivity(),drawerlayout,tool_bar,R.string.draweropen,R.string.drawerclose){
            @覆盖
            公共无效onDrawerOpened(查看drawerView){
                super.onDrawerOpened(drawerView);
            }            @覆盖
            公共无效onDrawerClosed(查看drawerView){
                super.onDrawerClosed(drawerView);
            }
        };
    mDrawer_layout.setDrawerListener(mDrawer_toogle);    }    / **
     *此接口必须由包含此活动的实施
     *片段,以允许在该片段的相互作用来传达
     *中包含的活性和潜在的其它片段
     *活动。
     * LT; P />
     *请参阅Android的培训课< A HREF =
     *http://developer.android.com/training/basics/fragments/communicating.html
     *>在其他片段&所述通信; / A>了解更多信息。
     * /
    公共接口OnFragmentInteractionListener {
        // TODO:更新参数类型和名称
        公共无效onFragmentInteraction(URI URI);
    }}

这是我的main_activity_layout:

 < android.support.v4.widget.DrawerLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / widget_drawer_layout
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>
    <包括
        机器人:ID =@ + ID /工具栏
        布局=@布局/工具栏
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =match_parent>    < / - 包括GT;
< RelativeLayout的
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=matrialdesign.practice.com.materialdesignpractice.MaterialDesign>< / RelativeLayout的>   <片段
       机器人:ID =@ + ID /片段
       机器人:layout_width =280dp
       机器人:layout_height =match_parent
       机器人:layout_gravity =开始
       机器人:名字=matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer
       工具:布局=@布局/ fragment_navigatoin_drawer/>       应用:布局=@布局/ fragment_navigatoin_drawer/>
< /android.support.v4.widget.DrawerLayout>

这是我的navigation_drawer_xml:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer>    < - TODO:更新空白片段布局 - >
    <的TextView
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:文字=@字符串/ hello_blank_fragment/>< / RelativeLayout的>
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer>< - TODO:更新空白片段布局 - >
<的TextView
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:文字=@字符串/ hello_blank_fragment/>< / RelativeLayout的>

这是我的logcat的:

  16 03-11:32:27.018 9974-9974 / matrialdesign.practice.com.materialdesignpractice E / AndroidRuntime:致命异常:主要
    工艺:matrialdesign.practice.com.materialdesignpractice,PID:9974
    了java.lang.RuntimeException:无法启动活动ComponentInfo{matrialdesign.practice.com.materialdesignpractice/matrialdesign.practice.com.materialdesignpractice.MaterialDesign}: android.view.InflateException:二进制XML文件行#21:错误充气类片段
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
            在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            在android.app.ActivityThread.access $ 800(ActivityThread.java:151)
            在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)
            在android.os.Handler.dispatchMessage(Handler.java:110)
            在android.os.Looper.loop(Looper.java:193)
            在android.app.ActivityThread.main(ActivityThread.java:5292)
            在java.lang.reflect.Method.invokeNative(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:515)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:824)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            在dalvik.system.NativeStart.main(本机方法)
     android.view.InflateException:引起二进制XML文件行#21:错误充气类片段
            在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
            在android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            在android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            在android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            在android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            在android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            在android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            在matrialdesign.practice.com.materialdesignpractice.MaterialDesign.onCreate(MaterialDesign.java:18)
            在android.app.Activity.performCreate(Activity.java:5264)
            在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
在android.app.ActivityThread.access $ 800(ActivityThread.java:151)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)
在android.os.Handler.dispatchMessage(Handler.java:110)
在android.os.Looper.loop(Looper.java:193)
在android.app.ActivityThread.main(ActivityThread.java:5292)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:515)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:824)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
在dalvik.system.NativeStart.main(本机方法)
     java.lang.ClassCastException:引起matrialdesign.practice.com.materialdesignpractice.MaterialDesign@41ce3390必须实现OnFragmentInteractionListener
            在matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer.onAttach(navigatoin_drawer.java:90)
            在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849)
            在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
            在android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
            在android.app.Activity.onCreateView(Activity.java:4819)
            在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
在android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
在android.view.LayoutInflater.inflate(LayoutInflater.java:492)
在android.view.LayoutInflater.inflate(LayoutInflater.java:397)
在android.view.LayoutInflater.inflate(LayoutInflater.java:353)
在android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
在android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
在matrialdesign.practice.com.materialdesignpractice.MaterialDesign.onCreate(MaterialDesign.java:18)
在android.app.Activity.performCreate(Activity.java:5264)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
在android.app.ActivityThread.access $ 800(ActivityThread.java:151)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)
在android.os.Handler.dispatchMessage(Handler.java:110)
在android.os.Looper.loop(Looper.java:193)
在android.app.ActivityThread.main(ActivityThread.java:5292)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:515)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:824)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
在dalvik.system.NativeStart.main(本机方法)


解决方案

在哪里片段定义?您正在使用中,需要一些标记库定义布局片​​段标记。对吧?

我认为你应该使用抽屉布局内的FrameLayout显示的抽屉里。

喜欢的东西以下可以使用

 < android.support.v4.widget.DrawerLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID / drawer_layout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_below =@ + ID / main_toolbar>        <! - 的主要内容视图 - >        <的FrameLayout
            机器人:ID =@ + ID /容器
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent/>        <! - 左导航抽屉 - >        < ListView控件
            机器人:ID =@ + ID / left_drawer
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_gravity =左
            机器人:阿尔法=75
            机器人:背景=#111
            机器人:choiceMode =singleChoice
            机器人:分=@机器人:彩色/透明
            机器人:dividerHeight =12dp
            机器人:paddingTop =8DP/>
    < /android.support.v4.widget.DrawerLayout>

I am creating an android application in android studio that consists of navigation drawer. I am getting an error called "java.lang.ClassCastException: matrialdesign.practice.com.materialdesignpractice.MaterialDesign@41cdeb78 must implement OnFragmentInteractionListener at matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer.onAttach(navigatoin_drawer.java:90)"

Please help me to solve it i am new to android navigation drawer this is my main activity

package matrialdesign.practice.com.materialdesignpractice;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;


public class MaterialDesign extends ActionBarActivity
{
    Toolbar tool_bar_widget;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_material_design);
        tool_bar_widget = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(tool_bar_widget);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        navigatoin_drawer navigation = (navigatoin_drawer)getFragmentManager().findFragmentById(R.id.fragment);
        navigation.setup((DrawerLayout)findViewById(R.id.widget_drawer_layout),tool_bar_widget);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_material_design, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if(id == R.id.custom_nav_icon)
        {
            Intent navigation = new Intent(MaterialDesign.this,Navigationactivity.class);
            startActivity(navigation);
        }

        return super.onOptionsItemSelected(item);
    }
}

This is my navigation drawer activity:

package matrialdesign.practice.com.materialdesignpractice;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link navigatoin_drawer.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link navigatoin_drawer#newInstance} factory method to
 * create an instance of this fragment.
 */
public class navigatoin_drawer extends Fragment
{
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private ActionBarDrawerToggle mDrawer_toogle;
    public DrawerLayout mDrawer_layout;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment navigatoin_drawer.
     */
    // TODO: Rename and change types and number of parameters
    public static navigatoin_drawer newInstance(String param1, String param2) {
        navigatoin_drawer fragment = new navigatoin_drawer();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public navigatoin_drawer() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_navigatoin_drawer, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public void setup(DrawerLayout drawerlayout,Toolbar tool_bar)
    {
        mDrawer_layout=drawerlayout;
        mDrawer_toogle = new ActionBarDrawerToggle(getActivity(),drawerlayout,tool_bar,R.string.draweropen,R.string.drawerclose){


            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
    mDrawer_layout.setDrawerListener(mDrawer_toogle);

    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

}

This is my main_activity_layout:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_drawer_layout"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

    </include>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="matrialdesign.practice.com.materialdesignpractice.MaterialDesign">

</RelativeLayout>

   <fragment
       android:id="@+id/fragment"
       android:layout_width="280dp"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       android:name="matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer"
       tools:layout="@layout/fragment_navigatoin_drawer" />

       app:layout="@layout/fragment_navigatoin_drawer"/>
</android.support.v4.widget.DrawerLayout>

This is my navigation_drawer_xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

</RelativeLayout>

This is my logcat:

03-11 16:32:27.018    9974-9974/matrialdesign.practice.com.materialdesignpractice E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: matrialdesign.practice.com.materialdesignpractice, PID: 9974
    java.lang.RuntimeException: Unable to start activity ComponentInfo{matrialdesign.practice.com.materialdesignpractice/matrialdesign.practice.com.materialdesignpractice.MaterialDesign}: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at matrialdesign.practice.com.materialdesignpractice.MaterialDesign.onCreate(MaterialDesign.java:18)
            at android.app.Activity.performCreate(Activity.java:5264)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: matrialdesign.practice.com.materialdesignpractice.MaterialDesign@41ce3390 must implement OnFragmentInteractionListener
            at matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer.onAttach(navigatoin_drawer.java:90)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
            at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
            at android.app.Activity.onCreateView(Activity.java:4819)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at matrialdesign.practice.com.materialdesignpractice.MaterialDesign.onCreate(MaterialDesign.java:18)
            at android.app.Activity.performCreate(Activity.java:5264)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)
解决方案

Where is the fragment defined? You are using the fragment tag in the layout which needs some tag lib definition. Right?

I think you should use a framelayout inside the drawer layout to display the drawer.

Something like following you can use

<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"
        android:layout_below="@+id/main_toolbar" >

        <!-- The main content view -->

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

        <!-- Left navigation drawer -->

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:alpha=".75"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="12dp"
            android:paddingTop="8dp" />
    </android.support.v4.widget.DrawerLayout>

这篇关于修复java.lang.ClassCastException在Android的抽屉式导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 21:34