gpt4 book ai didi

java - 工具栏 findViewById 返回空指针

转载 作者:搜寻专家 更新时间:2023-11-01 07:42:53 34 4
gpt4 key购买 nike

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

  • 禁用默认操作栏。我做了....
  • 将 findViewById 放在 setContent 之后。我做了...
  • 将布局包含在主要 Activity 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);
}

....

最佳答案

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

去掉这里的id字段,在my_toolbar布局中重新定义了

关于java - 工具栏 findViewById 返回空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51713332/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com