工具栏findViewById返回空指针

时间:2018-08-06 18:06:20

标签: java android

我已经阅读了有关此问题的链接。 主要解决方案是:

  • 禁用默认操作栏。我做到了......
  • 在setContent之后放入findViewById。我做了...
  • 将布局包括在主活动layout.xml文件中。我做到了。

,但仍然返回空指针。我疯了,因为我也无法在预览中看到工具栏。

styles.xml

未设置操作栏。     

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

my_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar">

</android.support.v7.widget.Toolbar>

activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>

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

    <include
        android:id="@+id/my_toolbar"
        layout="@layout/my_toolbar"/>


    <!-- Add Google Maps Fragment to the MainActivity -->
    <fragment xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/my_toolbar"
        tools:context=".MapsActivity" />
        <!--android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"-->



    <Button
        android:id="@+id/btClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/send_screenshot" />


</RelativeLayout>

MapsActivity.java

....

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            // Get fused location client
            mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
            Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
            myToolbar.setTitle("dio porco dio");
            setSupportActionBar(myToolbar);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }

....

3 个答案:

答案 0 :(得分:2)

logger.LogInformation("Amount {0:$#,##0.00}", 100.0);

在此处删除id字段,它在my_toolbar布局中再次定义。

答案 1 :(得分:1)

在您的activity_maps.xml中 而不是编写

 <include
    android:id="@+id/my_toolbar"
    layout="@layout/my_toolbar"/>

写这个

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

或在您的包含标签中提供其他任何ID(但不提供此ID)

android:id="@+id/my_toolbar"

答案 2 :(得分:1)

在您的MapsActivity中将布局提取为一个视图,然后像这样在该视图中获取工具栏。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Get fused location client
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        //get that view of <include> tag
        //I suggest change the name to something else, same name for include and toolbar will be difficult to understand and read.
        View includeLayout = findViewById(R.id.my_toolbar) //id of include

        //from that includeToolbar you can get toolbar with the Id.            
        Toolbar myToolbar = (Toolbar) includeLayout.findViewById(R.id.my_toolbar);
        myToolbar.setTitle("dio porco dio");


        setSupportActionBar(myToolbar);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }