如何使我的应用与导航视图一起运行?

时间:2019-01-13 22:32:49

标签: android kotlin

我正在尝试将导航菜单添加到我的应用中,并且我遵循在https://developer.android.com/training/implementing-navigation/nav-drawer上创建导航抽屉的操作 当我尝试运行该应用程序时,我看到的是白屏,然后关闭了该应用程序。 我的代码没有错误,或者在编译时没有错误,只会加载主页。

package com.example.troubleshootonly.americanharvest

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.example.troubleshootonly.americanharvest.R.layout.activity_main

var pass: String = ""

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activity_main)

        //these create buttons with buttF being the name of the button
        //everything in brackets is the function, which starts activity
        /*
        buttF.setOnClickListener {
            startActivity(Intent(this, Activity2::class.java))
        }
        buttE.setOnClickListener {
            startActivity(Intent(this, Activity3::class.java))
        }
        buttC.setOnClickListener {
            startActivity(Intent(this, Activity4::class.java))
        }
        */

        /* admin stuff
        admin.setOnClickListener {
            val tempPw = adminPass.text.trim().toString()
            if (tempPw.equals(pass, true)) {
                startActivity(Intent(this, adminPage::class.java))
            } else {
                startActivity(Intent(this, MainActivity::class.java))
            }
        }
        */
    }
}

这是我的主要活动页面

xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mainPage"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:fitsSystemWindows="true" tools:openDrawer="start"
                                        android:visibility="visible">


    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/ahlogo" android:visibility="visible"/>

    <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_gravity="start"
            android:layout_height="wrap_content" android:id="@+id/menuMain"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginEnd="44dp" android:layout_marginRight="44dp"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintVertical_bias="0.0"
            app:menu="@menu/drawer_view" android:fitsSystemWindows="false">
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

logcat:

    --------- beginning of crash
2019-01-12 20:20:45.486 13641-13641/com.example.troubleshootonly.americanharvest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.troubleshootonly.americanharvest, PID: 13641
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.troubleshootonly.americanharvest/com.example.troubleshootonly.americanharvest.MainActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
     Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.troubleshootonly.americanharvest.MainActivity.onCreate(MainActivity.kt:13)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f0c0000
        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:216)
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2155)
        at android.content.res.Resources.getLayout(Resources.java:1155)
2019-01-12 20:20:45.487 13641-13641/com.example.troubleshootonly.americanharvest E/AndroidRuntime:     at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:125)
        at android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:264)
        at android.support.design.widget.NavigationView.<init>(NavigationView.java:189)
        at android.support.design.widget.NavigationView.<init>(NavigationView.java:104)
            ... 28 more


    --------- beginning of system

1 个答案:

答案 0 :(得分:0)

代码不清楚,也缺少一些代码行。就像我看不到抽屉布局的开始。 另外,您还没有在MainActivity.java中声明抽屉布局和NavigationView,因为您没有清楚地提供代码。因此,我为您提供了实现导航抽屉所需的主要文件。只需按原样复制粘贴(期望包名称:p)。

MainActivity.java(我将其命名为navBar.java)

public class navBar extends AppCompatActivity {

private DrawerLayout drawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav_bar);

    drawerLayout = findViewById(R.id.drawer_layout_tab_bar);

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

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            // set item as selected to persist highlight
            menuItem.setChecked(true);
            // close drawer when item is tapped
            drawerLayout.closeDrawers();

            // Add code here to update the UI based on the item selected
            // For example, swap UI fragments here

            return true;
        }
    });
}
}

activity_main.xml(我将其命名为activity_nav_bar)

public class navBar extends AppCompatActivity {

private DrawerLayout drawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav_bar);

    drawerLayout = findViewById(R.id.drawer_layout_tab_bar);

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

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            // set item as selected to persist highlight
            menuItem.setChecked(true);
            // close drawer when item is tapped
            drawerLayout.closeDrawers();

            // Add code here to update the UI based on the item selected
            // For example, swap UI fragments here

            return true;
        }
    });
}
}

将它们添加到您的应用gradle文件中(如果不存在)

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'

使用此菜单文件在nav_bar中显示内容。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_profile"
        android:icon="@drawable/ic_baseline_account_circle_24px"
        android:title="@string/profile" />
    <item
        android:id="@+id/nav_templates"
        android:icon="@drawable/ic_baseline_create_24px"
        android:title="@string/temp" />
    <item
        android:id="@+id/nav_logout"
        android:icon="@drawable/ic_baseline_exit_to_app_24px"
        android:title="@string/log_out" />
</group>
</menu>

希望这会有所帮助。