【问题标题】:Android findViewById for toolbar returning null用于返回 null 的工具栏的 Android findViewById
【发布时间】:2016-05-31 21:30:49
【问题描述】:

在我的 android 应用程序中,我使用的是自定义工具栏,而 findViewById() 似乎找不到该工具栏。

我已经在互联网上查看了所有与此类似的问题,但似乎没有一个有效。

我的屏幕总是返回 Toast “工具栏失败...”

这是我的 java onCreate():

protected Toolbar toolbar;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        if(toolbar != null) {
            setSupportActionBar(toolbar);
        } else {
            Toast.makeText(Home.this, "Toolbar failed...", Toast.LENGTH_SHORT).show();
            Toast.makeText(Home.this,"", Toast.LENGTH_SHORT).show();
        }
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                handleSendText(intent); // Handle text being sent
            }
        }

    }

这是 xml 布局文件:

<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" >


    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:id="@+id/tool_bar"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/home"
            android:layout_below="@id/tool_bar"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your Scripture:"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_below="@+id/textView2"
            android:layout_alignParentLeft="true" />

    <TextView
        android:id="@+id/scripturecontainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You have not selected a scripture yet..."
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_below="@+id/textView1"
        android:layout_alignParentLeft="true" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="trywidget"
        android:text="Try the Widget"
        android:layout_below="@+id/scripturecontainer"
        android:layout_alignParentLeft="true" />

    <Button
        android:id="@+id/widget_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="change"
        android:text="Select A Scripture"
        android:layout_below="@+id/button2"
        android:layout_alignParentLeft="true" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="suggest"
        android:text="Suggest A Feature"
        android:layout_below="@+id/widget_button"
        android:layout_alignParentLeft="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="wallpaper"
        android:text="Change the Wallpaper"
        android:layout_below="@+id/button3"
        android:layout_alignParentLeft="true" />
    </RelativeLayout>
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>

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

这是我的样式 xml 文件:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

【问题讨论】:

  • 这似乎不是您为 Activity 设置的布局,因为它没有 ID 为 containerViewGroup,而 FragmentTransaction 会导致第一次运行时没有那个崩溃。
  • 它不会崩溃。只返回一个空值。
  • 我不知道您所说的“返回 null”是什么意思,但如果当前布局没有 containerFragmentTransaction 会崩溃。鉴于此,以及findViewById() 为您的Toolbar 返回null 的事实,我想说您发布的布局不是activity_home
  • 我发布的布局是fragment_home。还有一个activity_home.xml。
  • 这行得通。谢谢!

标签: java android xml android-toolbar findviewbyid


【解决方案1】:
 <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>

使用上面的代码在你的xml中声明工具栏并更改

android:theme="your_theme"

【讨论】:

  • 这只是返回了一个异常,说它找不到视图android.support.design.widget.AppBarLayout。
  • @sub6resources 那么你的 build.gradle 中缺少 compile "com.android.support:design:23.2.1"
猜你喜欢
  • 1970-01-01
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
相关资源
最近更新 更多