【问题标题】:App crashes when I switch from a map activity to another activity, then back to the map activity当我从一个地图活动切换到另一个活动,然后再切换回地图活动时,应用程序崩溃
【发布时间】:2017-11-15 11:31:22
【问题描述】:

我有一个小应用程序,当按下“保存位置”按钮时,它会接收您当前的经纬度 GPS 坐标。在显示地图并标记您当前位置的位置开始另一个活动。在这个地图活动上,有一个按钮可以让你按到另一个活动(我们称之为备忘录活动),其中有一个按钮可以让你回到地图活动。但是,应用程序在按下此按钮时会崩溃,这会将您带回地图活动。

这里是主要活动、地图活动和备忘录活动的代码。

主要活动

public class HomeScreen extends AppCompatActivity {

private Button button;
private TextView textView;
private LocationManager locationManager;
private LocationListener locationListener;
private double longitude;
private double latitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homescreen);
    button = (Button) findViewById(R.id.button);
}

public void saveAndGo(View view) {
    GPStracker g = new GPStracker(getApplicationContext());
    Location l = g.getLocation();
    if (l != null)
    {
        longitude =  l.getLongitude();
        latitude  =  l.getLatitude();
    }

    Intent go = new Intent(this, MapsActivity.class);
    go.putExtra("longitude", longitude);
    go.putExtra("latitude", latitude);
    startActivity(go);
    finish();
}

地图活动

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private Double latitude;
private Double longitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // 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);

        latitude = getIntent().getExtras().getDouble("latitude");
        longitude = getIntent().getExtras().getDouble("longitude");
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    LatLng pos = new LatLng(latitude, longitude);
    mMap.addMarker(new MarkerOptions().position(pos).title("Your Location"));
    mMap.setMinZoomPreference(16.0f);
    mMap.setMaxZoomPreference(20.0f);
    mMap.setMyLocationEnabled(true);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(pos));
}

public void goMemos (View view) {
    Intent go = new Intent(this, MemoScreen.class);
    startActivity(go);
}

备忘录活动

public class MemoScreen extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memoscreen);
    }

    public void goMap (View view){
        Intent go = new Intent(this, MapsActivity.class);
        startActivity(go);
    }
}

我认为它崩溃了,因为当我从备忘录活动切换回地图活动时尝试从主活动获取数据时,数据为空。如果我从地图活动中删除 getExtras() 调用,该应用程序运行良好。

我该如何解决这个问题?

注意:我想避免在备忘录活动中使用finish(),因为我计划向这个应用程序添加更多屏幕,因此使用它可能会导致不良行为(即返回到错误的活动)。

【问题讨论】:

  • 分享你的崩溃日志
  • 监视器状态“引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'double android.os.BaseBundle.getDouble(java.lang.String)'”道歉如果这不是你要找的东西
  • 您的 lat 和 long 为 null 并且在从 memo 活动返回时无法转换为 double,请将其传递给 memo 活动并在返回时将其作为 bundle 传递或将其存储在 map 中的变量中活动并检查空值
  • 为什么要创建一个意图来创建地图活动的新实例,只需在 goMap() 中调用 onbackpress。或者,如果您想创建一个新实例,以便在调用备忘录活动时发送纬度和经度,并从备忘录活动返回到地图活动。

标签: android android-activity


【解决方案1】:

你必须把简单的条件是

 if(getIntent().getExtras() != null){

    latitude = getIntent().getExtras().getDouble("latitude");
    longitude = getIntent().getExtras().getDouble("longitude");
 }

只需在您的地图活动中使用它 并没有忘记初始化

的值
private Double latitude=123.45;   
private Double longitude=123.45;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多