Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
修复android导航抽屉中的java.lang.ClassCastException_Android_Navigation Drawer - Fatal编程技术网

修复android导航抽屉中的java.lang.ClassCastException

修复android导航抽屉中的java.lang.ClassCastException,android,navigation-drawer,Android,Navigation Drawer,我正在android studio中创建一个由导航抽屉组成的android应用程序。我收到一个名为“java.lang.ClassCastException:materialDesign.practice.com.materialdesignpractice”的错误。MaterialDesign@41cdeb78必须实现OnFragmentInteractionListener 在matrialdesign.practice.com.materialdesignpractice.navigato

我正在android studio中创建一个由导航抽屉组成的android应用程序。我收到一个名为“java.lang.ClassCastException:materialDesign.practice.com.materialdesignpractice”的错误。MaterialDesign@41cdeb78必须实现OnFragmentInteractionListener 在matrialdesign.practice.com.materialdesignpractice.navigatoin\u drawer.onAttach(navigatoin\u drawer.java:90)”上

请帮我解决这个问题我是android导航抽屉的新手这是我的主要活动

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);
    }
}
这是我的导航抽屉活动:

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);
    }

}
看看

这就是解释。主要是将活动附加到上面文档中描述的onAttach方法中


或者查看可能的重复

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

我认为应该在抽屉布局中使用框架布局来显示抽屉

像下面这样的东西你可以用

<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>


implement
OnFragmentInteractionListener
MaterialDesign
中我应该在哪里执行它,或者在FragmentActivity@shayanpourvatan
的主要活动中执行它,公共类MaterialDesign Extendes ActionBarActivity implement OnFragmentInteractionListener
谢谢兄弟,它工作得很好谢谢你的支持answer@shayanpourvatanwelcome,很乐意帮忙
<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>
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)
<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>