首页 > 解决方案 > 切换到活动时,我的 Android 应用程序崩溃

问题描述

我的应用程序有三个激活,它们是:第一个 - BMI 计算器,第二个 - 连接到数据库的医院呼叫,第三个当我按下地图图标时的地图活动(正在崩溃)。对于任何即将回答我的问题的人,请记下我的名字,并注意我在开发方面是个白痴,我实际上一无所知,所以如果你给我一个解决方案,我会很感激你向我解释初学者,谢谢。

我的代码:

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HospitalMap">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".HospitalMap" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:id="@+id/activity_map_BMI_text_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="bmiView"
            android:text="BMI"
            android:textColor="@color/text_color"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/activity_map_BMI_image_button"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="@+id/activity_map_BMI_image_button" />

        <TextView
            android:id="@+id/activity_map_emergency_text_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="emergencyView"
            android:text="EMERGENCY"
            android:textColor="@color/text_color"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="@+id/activity_map_BMI_text_button"
            app:layout_constraintEnd_toEndOf="@+id/activity_map_emergency_image_button"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="@+id/activity_map_emergency_image_button"
            app:layout_constraintTop_toTopOf="@+id/activity_map_BMI_text_button" />

        <TextView
            android:id="@+id/activity_map_map_text_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mapView2"
            android:text="MAP"
            android:textColor="@color/text_color"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="@+id/activity_map_emergency_text_button"
            app:layout_constraintEnd_toEndOf="@+id/activity_map_map_image_button"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="@+id/activity_map_map_image_button"
            app:layout_constraintTop_toTopOf="@+id/activity_map_emergency_text_button" />

        <ImageView
            android:id="@+id/activity_map_BMI_image_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:onClick="bmiView"
            app:layout_constraintBottom_toTopOf="@+id/activity_map_BMI_text_button"
            app:layout_constraintEnd_toStartOf="@+id/activity_map_emergency_image_button"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:srcCompat="@drawable/icons8_sports_mode_48px" />

        <ImageView
            android:id="@+id/activity_map_emergency_image_button"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:onClick="emergencyView"
            app:layout_constraintBottom_toBottomOf="@+id/activity_map_BMI_image_button"
            app:layout_constraintEnd_toStartOf="@+id/activity_map_map_image_button"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/activity_map_BMI_image_button"
            app:layout_constraintTop_toTopOf="@+id/activity_map_BMI_image_button"
            app:srcCompat="@drawable/icons8_hospital_3_48px" />

        <ImageView
            android:id="@+id/activity_map_map_image_button"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:onClick="mapView2"
            app:layout_constraintBottom_toBottomOf="@+id/activity_map_emergency_image_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/activity_map_emergency_image_button"
            app:layout_constraintTop_toTopOf="@+id/activity_map_emergency_image_button"
            app:srcCompat="@drawable/icons8_map_60px" />
    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

活动

package com.softwareclinics.ourhealth;

import android.content.Context;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class HospitalMap extends FragmentActivity implements OnMapReadyCallback {
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
    TextView txtLat;
    String lat;
    String provider;
    protected String latitude, longitude;
    protected boolean gps_enabled, network_enabled;

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.mapView);
        mapFragment.getMapAsync(this);

    }


    @Override
    public void onMapReady(GoogleMap googleMap) {


        mMap = googleMap;


        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.setMinZoomPreference(11);


        // Care National Hospital Marker
        LatLng care = new LatLng(24.6796703, 46.7268578);
        mMap.addMarker(new MarkerOptions().position(care).title("National Care Hospital"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(care));

        // Kingdom Hospital Marker
        LatLng Kingdom = new LatLng(24.8013866, 46.6549079);
        mMap.addMarker(new MarkerOptions().position(Kingdom).title("Kingdom Hospital"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(Kingdom));

        // Alhabib Hospital Marker
        LatLng Habib = new LatLng(24.7213231, 46.7747423);
        mMap.addMarker(new MarkerOptions().position(Habib).title("Alhabib Hospital"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(Habib));

        // Mouwasat Hospital Marker
        LatLng Mouwasat = new LatLng(24.7871558, 46.7328539);
        mMap.addMarker(new MarkerOptions().position(Mouwasat).title("Mouwasat Hospital"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(Mouwasat));

        // King Salman Hospital Marker
        LatLng Salman = new LatLng(24.6071859, 46.6420467);
        mMap.addMarker(new MarkerOptions().position(Salman).title("King Salman Hospital"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(Salman));

    }


    public void emergencyView(View view) {
        Intent intent = new Intent(this, EmergencyActivity.class);
        startActivity(intent);
    }

    public void mapView2(View view) {
        Toast.makeText(this, "You are in BMI now", Toast.LENGTH_LONG).show();
    }

    public void bmiView(View view) {
        Intent intent = new Intent(this, bmi.class);
        startActivity(intent);
    }

}

标签: android

解决方案


您没有在 onCreate 方法中引用正确的布局 ID,您应该像这样更改它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // 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);
}

通过使用R.id.mapView,您正在搜索不在 XML 中的布局,因此它会抛出NullPointerException.

moveCamera此外,每次您在地图中添加标记时,无需调用该方法onMapReady。您应该在显示地图时(在添加所有标记之后)在您想要关注的标记上调用它。


推荐阅读