【发布时间】:2017-12-20 19:04:09
【问题描述】:
我正在尝试创建一个具有简单 UI 的应用程序。它由一个活动组成,其中包括 2 个按钮、一个文本视图和一个占据一半屏幕的谷歌地图片段。我使用 Android Studio 提供的拖放元素创建了布局。
问题是当我尝试运行它时,我收到以下错误:
jjava.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
这是我的活动:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback{
RequestQueue requestQueue;
Button serviceButton;
TextView textViewService;
SensorResponse responseJson;
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestQueue = Volley.newRequestQueue(this);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapView2);
mapFragment.getMapAsync(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
textViewService = findViewById(R.id.textViewServiceOutput);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
setUpMap();
}
public void setUpMap(){
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
}}
我已经用谷歌搜索了错误并尝试了我遇到的解决方案,例如尝试SupportMapFragment,但它仍然不起作用。
以下是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.tarikh.myapplication.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="47dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="callTheService"
android:text="Call Service"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mapButton" />
<TextView
android:id="@+id/textViewServiceOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="128dp"
android:layout_marginRight="128dp"
android:layout_marginTop="16dp"
android:text="Parking Bay Checker"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/mapButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="callTheMap"
android:text="Call Map"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewServiceOutput" />
<com.google.android.gms.maps.MapView
android:id="@+id/mapView2"
android:layout_width="match_parent"
android:layout_height="244dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="44dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3" />
</android.support.constraint.ConstraintLayout>
编辑: 感谢您的输入,经过一点调整,我已经设法让它工作了。我用以下内容替换了我的活动布局上的地图:
<fragment
android:id="@+id/mapFrag"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="329dp"
android:layout_marginTop="164dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
【问题讨论】:
-
你能更新布局文件吗?
-
@R2R 用我的布局文件更新了问题
标签: java android google-maps