gpt4 book ai didi

java - 如何显示附加到 Activity 的 fragment 中的向上按钮?

转载 作者:行者123 更新时间:2023-12-02 01:43:59 25 4
gpt4 key购买 nike

由于我的 fragment 无法显示向上按钮,我该怎么办?我的 fragment 附加到一个 Activity

我被告知尝试 onSupportNavigateUp 方法,但无法从 fragment 访问它

在我的 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);
}

}

在我的 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();

}

编辑:

当我尝试 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 时我的 logcat关于 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) 

我的样式 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 fragment

<?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(...)

如果您的 Activity 的 XML 布局包含您定义的工具栏,您可以使用以下方式分配它:

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

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

完成此操作后,您应该能够调用:

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

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

关于java - 如何显示附加到 Activity 的 fragment 中的向上按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57473823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com