在我的IDE中,LinearLayout
-标记'被标记为红色,并且错误消息显示为:"Element LinearLayout must be declared"
。我的目录设置如下:
activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_first_fragment"
android:icon="@drawable/ic_1"
android:title="First"
tools:ignore="HardcodedText" />
<item
android:id="@+id/nav_second_fragment"
android:icon="@drawable/ic_2"
android:title="Second"
tools:ignore="HardcodedText" />
<item
android:id="@+id/nav_third_fragment"
android:icon="@drawable/ic_3"
android:title="Third"
tools:ignore="HardcodedText" />
</group>
</menu>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<ImageButton
android:id="@+id/twitchBtn"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="124dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="centerInside"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/twitch" />
<ImageButton
android:id="@+id/youtubeBtn"
android:layout_width="253dp"
android:layout_height="249dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/twitchBtn"
app:layout_constraintVertical_bias="0.336"
app:srcCompat="@drawable/youtube" />
<TextView
android:id="@+id/textPlattform"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Choose platform"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="38sp"
app:fontFamily="sans-serif"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- This DrawerLayout has two children at the root -->
<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">
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_weight="1"
android:background="@android:color/white"
app:menu="@menu/drawer_view.xml"/>
<!-- This LinearLayout represents the contents of the screen -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="@+id/flContent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我想获得一个汉堡菜单,我正在学习本教程:https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer,但是如果我现在运行该程序,我会得到一个NullPointerExeption
,但是我认为这是由于drawer_view.xml
造成的工作。
答案 0 :(得分:1)
是因为抽屉_view.xml的定义错误
drawer_view.xml是
<?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_first_fragment"
android:icon="@drawable/ic_1"
android:title="First"
tools:ignore="HardcodedText" />
<item
android:id="@+id/nav_second_fragment"
android:icon="@drawable/ic_2"
android:title="Second"
tools:ignore="HardcodedText" />
<item
android:id="@+id/nav_third_fragment"
android:icon="@drawable/ic_3"
android:title="Third"
tools:ignore="HardcodedText" />
</group>
</menu>
在activity_main.xml中,菜单不包含扩展名.xml