充气类android.support.design.widget.NavigationView[启动时崩溃]时出错



应用程序应该有一个导航抽屉,从左边拉出并显示各种活动,但一旦导航栏添加到XML activity_homescreen文档中,应用程序一启动就会崩溃。

主页屏幕.java

package com.t99sdevelopment.centralized;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Toolbar;
public class HomeScreen extends AppCompatActivity {
    /*
    Intent intentHome = new Intent(this, HomeScreen.class);
    Intent intentAnnouncements = new Intent(this, AnnouncementsScreen.class);
    Intent intentSchedule = new Intent(this, ScheduleScreen.class);
    Intent intentCalendar = new Intent(this, CalendarScreen.class);
    Intent intentContactBook = new Intent(this, ContactBookScreen.class);
    Intent intentSportsSchedule = new Intent(this, SportsScheduleScreen.class);
    Intent intentFrontAndCentral = new Intent(this, FrontAndCentralScreen.class);
    Intent intentMap = new Intent(this, MapScreen.class);
    Intent intentAccount = new Intent(this, AccountScreen.class);
    */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homescreen);
        setTheme(R.style.AppTheme);
    }
    /*
    private void goToHome(View view){ startActivity(intentHome ); }
    private void goToAnnouncements(View view){ startActivity(intentAnnouncements ); }
    private void goToSchedule(View view){ startActivity(intentSchedule); }
    private void goToCalendar(View view){ startActivity(intentCalendar); }
    private void goToContactBook(View view){ startActivity(intentContactBook); }
    private void goToSportsSchedule(View view){ startActivity(intentSportsSchedule); }
    private void goToFrontAndCentral(View view){ startActivity(intentFrontAndCentral); }
    private void goToMap(View view){ startActivity(intentMap); }
    private void goToAccount(View view){ startActivity(intentAccount); }
    */
}

activity_homescreen.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar"
    android:background="@color/trojanBlack">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal">
        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/actionbar" />
        <ImageView
            android:id="@+id/ImageView_trojanHead"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="150dp"
            android:src="@drawable/trojan"
            android:scaleType="centerCrop" />
        <TextView
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:layout_marginTop="100dp"
            android:text="School is"
            android:paddingTop="5dp"
            android:textSize="30sp"
            android:textColor="@color/trojanBlack"
            android:textAlignment="center"
            android:background="@drawable/schoolbutton"
            android:radius="5dp"/>
        <RelativeLayout
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:layout_marginTop="-100dp">
            <Button
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:background="@drawable/refresh"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"/>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_centerInParent="true"
                android:text="OPEN"
                android:textSize="40sp"
                android:textColor="@color/trojanBlack"
                android:textAlignment="center"
                android:paddingTop="45dp" />
        </RelativeLayout>
        <TextView
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:background="#FFFFFF"
            android:layout_marginTop="10dp" />
        <FrameLayout
            android:id="@+id/flContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:menu="@menu/navigationdraweritems"
        app:headerLayout="@layout/nav_header" />
</android.support.v4.widget.DrawerLayout>

有了这些文件,应用程序就会崩溃,但如果我按如下方式更改activity_homescreen.xml(注释掉导航视图小部件)。。。

activity_homescreen.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar"
    android:background="@color/trojanBlack">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal">
        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/actionbar" />
        <ImageView
            android:id="@+id/ImageView_trojanHead"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="150dp"
            android:src="@drawable/trojan"
            android:scaleType="centerCrop" />
        <TextView
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:layout_marginTop="100dp"
            android:text="School is"
            android:paddingTop="5dp"
            android:textSize="30sp"
            android:textColor="@color/trojanBlack"
            android:textAlignment="center"
            android:background="@drawable/schoolbutton"
            android:radius="5dp"/>
        <RelativeLayout
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:layout_marginTop="-100dp">
            <Button
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:background="@drawable/refresh"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"/>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_centerInParent="true"
                android:text="OPEN"
                android:textSize="40sp"
                android:textColor="@color/trojanBlack"
                android:textAlignment="center"
                android:paddingTop="45dp" />
        </RelativeLayout>
        <TextView
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:background="#FFFFFF"
            android:layout_marginTop="10dp" />
        <FrameLayout
            android:id="@+id/flContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <!--
    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:menu="@menu/navigationdraweritems"
        app:headerLayout="@layout/nav_header" />
    -->
</android.support.v4.widget.DrawerLayout>

除了导航抽屉没有包含/工作之外,一切都很好。有人遇到过这个问题吗,或者更好的是,有人知道如何解决它吗?我需要导航抽屉工作,但它似乎会使我的应用程序崩溃。

logcat溢出了最大字符数,所以这里是github的要点。

最明显的问题是缺少一个方法,来自您的logcat:

Caused by: android.view.InflateException: Couldn't resolve menu item onClick handler goToHome in class com.t99sdevelopment.centralized.HomeScreen
Caused by: java.lang.NoSuchMethodException: goToHome [interface android.view.MenuItem]

最新更新