繁体   English   中英

如何从附加到活动的片段中显示向上按钮?

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

由于我的片段我无法显示向上按钮,该怎么办? 我的片段已附加到活动中

有人告诉我尝试onSupportNavigateUp方法,但无法从片段访问它

在我的片段

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);
    }

}

在我的活动中

    @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();

    }

编辑:

我尝试getSupportActionBar()。setDisplayHomeAsUpEnabled(true)时的日志猫; 活动中

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) 

我的样式xml文件

<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>

我的工具栏xml文件

<?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"/>

我的activity_main xml文件

<?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"/>

片段首页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>

NullPointerException可以通过不具有从ActionBar父级继承的主题或通过将自定义Toolbar用作ActionBar而不调用setSupportActionBar(...)

如果您的“活动”的XML布局包含您已定义的Toolbar ,则可以为其分配以下内容:

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

或者,您可以更改themes.xmlstyles.xml并使Activity主题的父主题成为名称中包含ActionBar的主题。

完成此操作后,您应该可以致电:

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

所有这些都可以在您的Activity的onCreate()内部进行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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