我已经阅读了关于这个问题的链接。主要的解决方案是:
- 禁用默认操作栏。我做了...
- 把findViewById放在setContent后面。我做了...
- 将布局包含在主活动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);
}
....
4条答案
按热度按时间67up9zun1#
<include android:id="@+id/my_toolbar" layout="@layout/my_toolbar"/>
删除这里的id字段,它在my_toolbar布局中再次定义。
m1m5dgzv2#
在activity_maps.xml中,而不是编写
写这个
或在include标记中提供除此之外的任何其他ID
lsmd5eda3#
提取布局作为
MapsActivity
中的视图,然后形成该视图,如下图所示的获取工具栏。mec1mxoz4#
本部分
在意大利,这是一种宗教冒犯。
非常糟糕。