简体   繁体   中英

how to display the up button from a fragment attached to an activity?

I can not display the up button since my fragment, how can I do? My fragment is attached to an activity

I was told to try the onSupportNavigateUp method but it is not accessible from a fragment

In my fragment

public class SearchFragment extends Fragment {

    public SearchFragment() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.search_and_notification, container, false);
    }

}

In my activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.configureAndShowHomeFragment();

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //3 - Handle actions on menu items
        switch (item.getItemId()) {
            case R.id.menu_activity_main_params:
                Toast.makeText(this, "Il n'y a rien à paramétrer ici, passez votre chemin...", Toast.LENGTH_LONG).show();
                return true;
            case R.id.menu_activity_main_search:
                launchSearchFragment();
                Toast.makeText(this, "Recherche indisponible, demandez plutôt l'avis de Google, c'est mieux et plus rapide.", Toast.LENGTH_LONG).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }



    private void launchSearchFragment() {
        searchFragment = new SearchFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_layout_main , searchFragment)
                .addToBackStack(null)
                .commit();

    }

Edit:

My logcat when i tried getSupportActionBar().setDisplayHomeAsUpEnabled(true); on Activity

08-13 10:38:53.813 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.siadous.thomas.mynews, PID: 15823
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.siadous.thomas.mynews/com.siadous.thomas.mynews.Activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
        at com.siadous.thomas.mynews.Activities.MainActivity.onCreate(MainActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

My style xml file

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/lightBlueColor</item>
        <item name="colorPrimaryDark">@color/blueColor</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!-- TOOLBAR THEME -->
    <style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <!-- Color of the title text in the Toolbar -->
        <item name="android:textColorPrimary">@color/colorAccent</item>
    </style>

    <!-- TOOLBAR TITLE THEME -->
    <style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
        <item name="android:textSize">21sp</item>
        <item name="android:textStyle">normal</item>
    </style>

</resources>

My Toolbar xml file

<?xml version="1.0" encoding="utf-8"?>
<!-- toolbar statement -->
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"

    android:theme="@style/ToolbarTheme"
    app:titleTextAppearance="@style/Toolbar.TitleText"/>

my activity_main xml file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame_layout_main"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

Fragment home xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".Fragments.HomeFragment"
    android:id="@+id/linear_layout_home_fragment">

    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.TabLayout
        android:id="@+id/activity_main_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/lightBlueColor"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabTextColor="@color/colorAccent"
        app:tabSelectedTextColor="@color/colorAccent"
        app:tabPaddingStart="-1dp"
        app:tabPaddingEnd="-1dp"/>

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Your NullPointerException can be explained by either not having a theme that inherits from an ActionBar parent, or by using a custom Toolbar as your ActionBar, but not calling setSupportActionBar(...)

If the XML layout for your Activity includes a Toolbar you have defined, you can assign it with:

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Alternatively, you can change your themes.xml or styles.xml and have the parent theme of the theme for your Activity be one that contains ActionBar in its name.

Once this is done, you should be able to call:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

All of this can take place inside onCreate() of your Activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM