A H*_*han 2 android android-fragments android-studio
正如标题所说,我想在我的'MapsActivity.java'中添加一个后退按钮到操作栏,每当点击后退按钮时,我希望用户转到父活动.我尝试使用这里提到的代码:添加回操作栏的按钮
现在每当我到达此页面时,我都会收到一条消息"很遗憾应用已经停止".注意:当我使用以下代码时,应用程序才会崩溃:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml: -
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
<!--
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>
Run Code Online (Sandbox Code Playgroud)
后退按钮应该已经存在而不添加它.如果它不存在,那么这意味着你没有在中提到父活动Android Manifest.
选项1:
非编程方式,您可以将清单文件中的元素添加为
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
Run Code Online (Sandbox Code Playgroud)
选项2:
在您的onCreate()方法中,插入
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Run Code Online (Sandbox Code Playgroud)
覆盖onSupportNavigateUp()方法:
@Override
public boolean onSupportNavigateUp(){
//code it to launch an intent to the activity you want
finish();
return true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2881 次 |
| 最近记录: |