后退按钮 - Android工作室

莫道桑榆晚,为霞尚满天。这篇文章主要讲述后退按钮 - Android工作室相关的知识,希望能为你提供帮助。
【后退按钮 - Android工作室】我有一个导航抽屉,我正在尝试用抽屉中选择的活动中的后退按钮实现操作栏(第二个活动)。我尝试将[getSupportActionBar()...]放在第二个活动的第一个Override中,但是当我从抽屉中选择活动时,它会让我崩溃。
谢谢
这是我的第一个活动

package com.progetto.app; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class Alarm extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alarm); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); }@Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } }@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.home, menu); return true; }@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; }return super.onOptionsItemSelected(item); }@SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); switch (id) {case R.id.nav_home: Intent h = new Intent(Alarm.this, Home.class); startActivity(h); break; case R.id.nav_bt: Intent i = new Intent(Alarm.this, BluetoothActivitySetting.class); startActivity(i); break; case R.id.nav_alarm: Intent g = new Intent(Alarm.this, AlarmActivity.class); startActivity(g); break; case R.id.nav_tips: Intent s = new Intent(Alarm.this, Tips.class); startActivity(s); break; } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; }}

这是我的第二项活动
package com.progetto.app; import android.annotation.TargetApi; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; import java.util.Calendar; import java.util.Random; import android.annotation.TargetApi; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import java.util.Calendar; public class AlarmActivity extends AppCompatActivity {//to make our alarm manager AlarmManager alarm_manager; TimePicker alarm_timepicker; TextView update_text; Context context; PendingIntent pending_intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alarm2); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); this.context = this; // initialize our alarm manager alarm_manager = (AlarmManager) getSystemService(ALARM_SERVICE); //initialize our timepicker alarm_timepicker = (TimePicker) findViewById(R.id.time_picker); //initialize our text update box update_text = (TextView) findViewById(R.id.update_text); // create an instance of a calendar final Calendar calendar = Calendar.getInstance(); // create an intent to the Alarm Receiver class final Intent my_intent = new Intent(this.context, Alarm_Receiver.class); // initialize start button Button alarm_on = (Button) findViewById(R.id.alarm_on); // create an onClick listener to start the alarm alarm_on.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {// setting calendar instance with the hour and minute that we picked // on the time picker calendar.set(Calendar.HOUR_OF_DAY, alarm_timepicker.getHour()); calendar.set(Calendar.MINUTE, alarm_timepicker.getMinute()); // get the int values of the hour and minute int hour = alarm_timepicker.getHour(); int minute = alarm_timepicker.getMinute(); // convert the int values to strings String hour_string = String.valueOf(hour); String minute_string = String.valueOf(minute); // method that changes the update text Textbox set_alarm_text("Alarm set to: " + hour_string + ":" + minute_string); // put in extra string into my_intent // tells the clock that you pressed the "alarm on" button my_intent.putExtra("extra", "alarm on"); // create a pending intent that delays the intent // until the specified calendar time pending_intent = PendingIntent.getBroadcast(AlarmActivity.this, 0, my_intent, PendingIntent.FLAG_UPDATE_CURRENT); // set the alarm manager alarm_manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent); }}); // initialize the stop button Button alarm_off = (Button) findViewById(R.id.alarm_off); // create an onClick listener to stop the alarm or undo an alarm setalarm_off.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {// method that changes the update text Textbox set_alarm_text("Alarm off!"); // cancel the alarm alarm_manager.cancel(pending_intent); my_intent.putExtra("extra", "alarm off"); // stop the ringtone sendBroadcast(my_intent); } }); }private void set_alarm_text(String output) { update_text.setText(output); }@Override public boolean onOptionsItemSelected(MenuItem item){ int id = item.getItemId(); if (id== android.R.id.home){ this.finish(); } return super.onOptionsItemSelected(item); } @Override public void onDestroy() { super.onDestroy(); Log.e("MyActivity", "on Destroy"); } }

这是第二个活动的xml
< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> < TimePicker android:id="@+id/time_picker" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:timePickerMode="clock" /> < Button android:id="@+id/alarm_on" android:layout_width="181dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" android:layout_marginBottom="66dp" android:text="@string/AlarmOn" /> < Button android:id="@+id/alarm_off" android:layout_width="180dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_marginBottom="55dp" android:text="@string/AlarmOff" /> < TextView android:id="@+id/update_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_below="@+id/time_picker" android:text="@string/SetAlarm" android:textAlignment="center" android:textSize="24sp" /> < /RelativeLayout>

答案首先,您没有在第二个活动布局中包含工具栏。您需要包含工具栏。这是我使用的解决方案,它没有问题。
在你的onCreate中添加它
initUI();

并在onCreate之后添加
@Override public boolean onNavigateUp() { onBackPressed(); return true; }public void initUI() { toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); toolbar.setNavigationOnClickListener(v -> onNavigateUp()); }

如果您有任何挑战,请告诉我。

    推荐阅读