我在android studio中创建了一个由导航抽屉组成的android应用程序。我收到一个名为的错误“java.lang.ClassCastException:matrialdesign.practice.com.materialdesignpractice.MaterialDesign@41cdeb78必须实现OnFragmentInteractionListener at matrialdesign.practice.com.materialdesignpractice.navigatoin_drawer.onAttach(navigatoin_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);
}
}
这是我的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>
这是我的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>
这是我的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)
答案 0 :(得分:1)
查看http://developer.android.com/training/basics/fragments/communicating.html
有解释。主要是在onAttach方法中附加活动,如上文所述。
答案 1 :(得分:1)
片段定义在哪里?您正在布局中使用片段标记,这需要一些标记库定义。正确?
我认为您应该在抽屉布局中使用framelayout来显示抽屉。
如下所示,您可以使用
<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>