正如标题所说,我想在“MapsActivity.java”的操作栏中添加一个后退按钮,每当单击后退按钮时,我希望用户转到父 Activity 。我尝试使用此处提到的代码:add back button to action bar
现在,每当我到达此页面时,我都会收到一条消息“不幸的是应用程序已停止”。 注意:只有当我使用以下代码时,应用程序才会崩溃:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
MapsActivity.java :-
package com.example.haseeb.memorableplaces;
import android.app.ActionBar;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
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 MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@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);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Intent i = getIntent();
Log.i("locationInfo", Integer.toString(i.getIntExtra("locationInfo", -1)));
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
}
AndroidManifest.xml:-
<?xml version="1.0" encoding="utf-8"?>
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:parentActivityName=".MainActivity"
android:label="@string/title_activity_maps"></activity>
</application>
最佳答案
后退按钮应该已经存在,无需您添加。如果它不存在,则意味着您没有在 Android Manifest
中提及父 Activity。
选项 1:
您可以以非编程方式将元添加到 list 文件中的 Activity
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
选项 2:
在您的 onCreate()
方法中,插入
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
重写onSupportNavigateUp()
方法:
@Override
public boolean onSupportNavigateUp(){
//code it to launch an intent to the activity you want
finish();
return true;
}
关于android - 将后退按钮添加到操作栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40439347/