-2
這不是我的代碼,但我正在處理包含兩個活動和一個下拉菜單的應用程序。您可以通過下拉菜單在兩項活動之間進行導航。我的目標是刪除其中一項活動和下拉菜單。我所完成的僅僅是從下拉菜單中刪除活動,以便它默認爲另一個,所以現在我需要刪除下拉菜單。但是,由於顯示的活動是通過下拉菜單定義的,因此如果我嘗試刪除下拉菜單,則只會繪製一個僅顯示操作欄的空白頁面。任何人都有從這個代碼判斷可能的解決方案?爲什麼刪除這個下拉菜單打破這個應用程序?
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.net.Uri;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
public class MainActivity extends FragmentActivity implements
\t \t ActionBar.OnNavigationListener {
\t
\t protected final String LOG = ".MainActivity";
\t
\t /**
\t * The serialization (saved instance state) Bundle key representing the
\t * current dropdown position.
\t */
\t private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
\t @Override
\t protected void onCreate(Bundle savedInstanceState) {
\t \t super.onCreate(savedInstanceState);
\t \t setContentView(R.layout.activity_main);
\t \t // Set up the action bar to show a dropdown list.
\t \t final ActionBar actionBar = getActionBar();
\t \t actionBar.setDisplayShowTitleEnabled(false);
\t \t actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
\t \t actionBar.setDisplayShowHomeEnabled(true);
\t \t actionBar.setIcon(R.drawable.ic_launcher);
\t \t // Set up the dropdown list navigation in the action bar.
\t \t actionBar.setListNavigationCallbacks(
\t \t // Specify a SpinnerAdapter to populate the dropdown list.
\t \t \t \t new ArrayAdapter<String>(getActionBarThemedContextCompat(),
\t \t \t \t \t \t android.R.layout.simple_list_item_1,
\t \t \t \t \t \t android.R.id.text1, new String[] {
\t \t \t \t \t \t \t \t //getString(R.string.title_section_recent),
\t \t \t \t \t \t \t \t getString(R.string.title_section_all), }), this);
\t \t
\t \t /**if(!getPreferences(Context.MODE_PRIVATE).getBoolean("disclaimer_accepted", false)) {
\t \t \t DialogFragment dialog = new DisclaimerDialogFragment();
\t \t \t dialog.show(getSupportFragmentManager(), "DisclaimerDialogFragment");
\t \t }**/
\t }
\t /**
\t * Backward-compatible version of {@link ActionBar#getThemedContext()} that
\t * simply returns the {@link android.app.Activity} if
\t * <code>getThemedContext</code> is unavailable.
\t */
\t @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
\t private Context getActionBarThemedContextCompat() {
\t \t if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
\t \t \t return getActionBar().getThemedContext();
\t \t } else {
\t \t \t return this;
\t \t }
\t }
\t @Override
\t public void onRestoreInstanceState(Bundle savedInstanceState) {
\t \t // Restore the previously serialized current dropdown position.
\t \t if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
\t \t \t getActionBar().setSelectedNavigationItem(
\t \t \t \t \t savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
\t \t }
\t }
\t
\t @Override
\t public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
\t }
\t
\t @Override
\t public boolean onOptionsItemSelected(MenuItem item) {
\t \t switch (item.getItemId()) {
\t \t case R.id.action_view_source:
\t \t \t Intent i2 = new Intent(Intent.ACTION_VIEW);
\t \t \t i2.setData(Uri.parse(this.getString(R.string.url_source)));
\t \t \t this.startActivity(i2);
\t \t \t return true;
\t \t case R.id.action_view_translation:
\t \t \t Intent i3 = new Intent(Intent.ACTION_VIEW);
\t \t \t i3.setData(Uri.parse(this.getString(R.string.url_translation)));
\t \t \t this.startActivity(i3);
\t \t \t return true;
\t \t \t
\t \t case R.id.action_view_bugs:
\t \t \t Intent i4 = new Intent(Intent.ACTION_VIEW);
\t \t \t i4.setData(Uri.parse(this.getString(R.string.url_bugs)));
\t \t \t this.startActivity(i4);
\t \t \t return true;
\t \t \t
\t \t default:
\t \t \t return super.onOptionsItemSelected(item);
\t \t }
\t }
\t @Override
\t public void onSaveInstanceState(Bundle outState) {
\t \t // Serialize the current dropdown position.
\t \t outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
\t \t \t \t .getSelectedNavigationIndex());
\t }
\t @Override
\t public boolean onNavigationItemSelected(int position, long id) {
\t \t // When the given dropdown item is selected, show its contents in the
\t \t // container view.
\t \t Fragment fragment = null;
\t \t switch(position) {
\t \t case 0:
\t \t \t fragment = new AllTasksListFragment();
\t \t \t break;
\t \t case 1:
\t \t \t //fragment = new RecentTaskListFragment();
\t \t \t break;
\t \t }
\t \t getSupportFragmentManager().beginTransaction()
\t \t \t \t .replace(R.id.container, fragment).commit();
\t \t return true;
\t }
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" />