实现导航抽屉

时间:2017-11-18 20:07:56

标签: java android listview android-fragments navigation-drawer

我想实现一个导航抽屉来显示一个列表视图,在内容中包含已经创建的活动与另一个列表视图,当我想运行应用程序时它崩溃了这个错误在logcat中: 其中一个错误是在setContentView的抽屉java类的onCreate中。我是从一个片段开始抽屉活动的,这引出了一个问题,如果这是问题吗?我好像没有发现问题。

  

11-18 20:30:48.772 23674-23674 / com.brainstorm.brainstormv1 E / AndroidRuntime:> FATAL EXCEPTION:main

     
    

流程:com.brainstorm.brainstormv1,PID:23674     java.lang.RuntimeException:无法启动活动> ComponentInfo {com.brainstorm.brainstormv1 / com.brainstorm.brainstormv1.Activitie> s.IdeasActivityDrawer}:android.view.InflateException:二进制XML文件行#0:>二进制XML文件行#0:错误膨胀类片段     在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)     在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)     在android.app.ActivityThread.-wrap14(ActivityThread.java)     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1635)     在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                      在android.os.Looper.loop(Looper.java:154)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:6692)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)                                                                                   引起:android.view.InflateException:二进制XML文件行#0:二进制XML文件行#0:错误膨胀类片段                                                                                   引起:android.view.InflateException:二进制XML文件行#0:错误膨胀类片段                                                                                   引起:java.lang.NullPointerException                                                                                      at java.lang.VMClassLoader.findLoadedClass(Native Method)                                                                                      at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:742)                                                                                      at java.lang.ClassLoader.loadClass(ClassLoader.java:362)                                                                                      在java.lang.ClassLoader.loadClass(ClassLoader.java:312)                                                                                      在android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:499)                                                                                      在android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3637)                                                                                      在android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)                                                                                      在android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)                                                                                      在android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)                                                                                      在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)                                                                                      在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:784)                                                                                      在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)                                                                                      在android.view.LayoutInflater.rInflate(LayoutInflater.java:865)                                                                                      在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)                                                                                      在android.view.LayoutInflater.rInflate(LayoutInflater.java:873)                                                                                      在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)                                                                                      在android.view.LayoutInflater.parseInclude(LayoutInflater.java:1011)                                                                                      在android.view.LayoutInflater.rInflate(LayoutInflater.java:861)                                                                                      在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)                                                                                      在android.view.LayoutInflater.parseInclude(LayoutInflater.java:1011)                                                                                      在android.view.LayoutInflater.rInflate(LayoutInflater.java:861)                                                                                      在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)                                                                                      在android.view.LayoutInflater.parseInclude(LayoutInflater.java:1011)                                                                                      在android.view.LayoutInflater.rInflate(LayoutInflater.java:861)                                                                                      在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)                                                                                      在android.view.LayoutInflater.inflate(LayoutInflater.java:525)                                                                                      在android.view.LayoutInflater.inflate(LayoutInflater.java:427)                                                                                      在android.view.LayoutInflater.inflate(LayoutInflater.java:378)                                                                                      在android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)                                                                                      在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)                                                                                      at com.brainstorm.brainstormv1.Activities.IdeasActivityDrawer.onCreate(IdeasActivityDrawer.java:32)                                                                                      在android.app.Activity.performCreate(Activity.java:6912)                                                                                      在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)                                                                                      在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)                                                                                      在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)                                                                                      在android.app.ActivityThread.-wrap14(ActivityThread.java)                                                                                      在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1635)     11-18 20:30:48.772 23674-23674 / com.brainstorm.brainstormv1 E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:102)                                                                                      在android.os.Looper.loop(Looper.java:154)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:6692)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

  

这是我的java类,所有内容都是使用google模板自动创建的,只是在抽屉中为listview添加了我的customadapter以及如何创建数据。

public class IdeasActivityDrawer extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ideas_drawer); //the line the error from the logcat points at
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    ListView groupUsers = (ListView) findViewById(R.id.lv_groupmembers);

    CustomAdapterContact adapter = new CustomAdapterContact(this, generateData());

    groupUsers.setAdapter(adapter);

    groupUsers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            if (position!=0)
            {
                Log.e("position", Integer.toString(position));

            }

        }

    });
    //navigationView.setNavigationItemSelectedListener(this);
}

private ArrayList<ModelContacts> generateData()
{
    ArrayList<ModelContacts> modelContacts = new ArrayList<ModelContacts>();
    modelContacts.add(new ModelContacts("Contacts"));
    modelContacts.add(new ModelContacts(R.drawable.avatar_circle_blue_120dp, "ContactName"));

    return modelContacts;
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.ideas_activity_drawer, 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;
    }

    return super.onOptionsItemSelected(item);
}

/*@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}*/

EDIT- XML代码: 活动本身:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_ideas_activity_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <ListView
        android:id="@+id/lv_groupmembers"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="55dp" />
</android.support.design.widget.NavigationView>

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

app_bar_ideas_activity:

<?xml version="1.0" encoding="utf-8"?>    <android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.brainstorm.brainstormv1.Activities.IdeasActivityDrawer">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_ideas_activity_drawer" />

content_ideas_activity:

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.brainstorm.brainstormv1.Activities.IdeasActivityDrawer"
tools:showIn="@layout/app_bar_ideas_activity_drawer">

<include
    layout="@layout/activity_ideas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

0 个答案:

没有答案