android 工具栏findViewById返回空指针

zbdgwd5y  于 2023-04-04  发布在  Android
关注(0)|答案(4)|浏览(157)

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

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

但仍然得到空指针回来。我越来越疯狂,因为我也看不到预览工具栏。

styles.xml

未设置操作栏。

  1. <!-- Base application theme. -->
  2. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  3. <!-- Customize your theme here. -->
  4. <item name="colorPrimary">@color/colorPrimary</item>
  5. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  6. <item name="colorAccent">@color/colorAccent</item>
  7. </style>
  8. </resources>

my_toolbar.xml

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

activity_maps.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <include
  7. android:id="@+id/my_toolbar"
  8. layout="@layout/my_toolbar"/>
  9. <!-- Add Google Maps Fragment to the MainActivity -->
  10. <fragment xmlns:tools="http://schemas.android.com/tools"
  11. android:id="@+id/map"
  12. android:name="com.google.android.gms.maps.SupportMapFragment"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:layout_below="@id/my_toolbar"
  16. tools:context=".MapsActivity" />
  17. <!--android:layout_alignParentStart="true"
  18. android:layout_alignParentTop="true"-->
  19. <Button
  20. android:id="@+id/btClear"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_alignParentBottom="true"
  24. android:layout_centerHorizontal="true"
  25. android:text="@string/send_screenshot" />
  26. </RelativeLayout>

MapsActivity.java

....

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_maps);
  5. // Get fused location client
  6. mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
  7. Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
  8. myToolbar.setTitle("dio porco dio");
  9. setSupportActionBar(myToolbar);
  10. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  11. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  12. .findFragmentById(R.id.map);
  13. mapFragment.getMapAsync(this);
  14. }

....

67up9zun

67up9zun1#

<include android:id="@+id/my_toolbar" layout="@layout/my_toolbar"/>
删除这里的id字段,它在my_toolbar布局中再次定义。

m1m5dgzv

m1m5dgzv2#

在activity_maps.xml中,而不是编写

  1. <include
  2. android:id="@+id/my_toolbar"
  3. layout="@layout/my_toolbar"/>

写这个

  1. <include
  2. layout="@layout/my_toolbar"/>

或在include标记中提供除此之外的任何其他ID

  1. android:id="@+id/my_toolbar"
lsmd5eda

lsmd5eda3#

提取布局作为MapsActivity中的视图,然后形成该视图,如下图所示的获取工具栏。

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_maps);
  5. // Get fused location client
  6. mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
  7. //get that view of <include> tag
  8. //I suggest change the name to something else, same name for include and toolbar will be difficult to understand and read.
  9. View includeLayout = findViewById(R.id.my_toolbar) //id of include
  10. //from that includeToolbar you can get toolbar with the Id.
  11. Toolbar myToolbar = (Toolbar) includeLayout.findViewById(R.id.my_toolbar);
  12. myToolbar.setTitle("my title");
  13. setSupportActionBar(myToolbar);
  14. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  15. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  16. .findFragmentById(R.id.map);
  17. mapFragment.getMapAsync(this);
  18. }
展开查看全部
mec1mxoz

mec1mxoz4#

本部分

  1. myToolbar.setTitle("dxx pxxxx dxx");

在意大利,这是一种宗教冒犯。
非常糟糕。

相关问题