gpt4 book ai didi

android - 将返回按钮添加到操作栏

转载 作者:太空狗 更新时间:2023-10-29 16:30:26 24 4
gpt4 key购买 nike

正如标题所说,我想在“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com