gpt4 book ai didi

android - Fragment 上的 SetSupportActionBar 未设置 SupportActionBar

转载 作者:行者123 更新时间:2023-11-30 00:18:01 25 4
gpt4 key购买 nike

以下示例 Android-Support/Fragments对于 MvvmCross 并尝试重构我自己的应用程序以将 AppBar 和 Toolbar 资源定义移动到 fragment 。我在 fragment 上调用 ​​SetSupportActionbar 时遇到问题,而该调用过去在 Activity 上运行良好。这是重构后的代码:

activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<!-- Actual content of the screen -->
<FrameLayout
android:id="@+id/main_content_frame"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_below="@+id/toolbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

主 Activity .cs

using Android.App;
using Android.OS;
using Android.Content.PM;
using MvvmCross.Droid.Support.V7.AppCompat;
using Mobile.Core.ViewModels;

namespace Mobile.Droid.Activities
{
[Activity(
Label = "@string/applicationName",
Icon = "@drawable/ic_icon",
Theme = "@style/AppTheme.Default",
LaunchMode = LaunchMode.SingleTop,
ScreenOrientation = ScreenOrientation.Portrait,
Name = "droid.mobile.activities.MainActivity")]
public class MainActivity : MvxAppCompatActivity<MainViewModel>
{
...
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

SetContentView(Resource.Layout.activity_main);

ViewModel.ShowLists();
}
....
}
}

fragment 列表.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/list_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.design.widget.AppBarLayout
android:id="@+id/main_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.ActionBar">

<android.support.v7.widget.Toolbar
android:id="@+id/main_tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.ToolBar"
app:layout_scrollFlags="scroll|enterAlways" />

</android.support.design.widget.AppBarLayout>

<MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout
android:id="@+id/listsRefresher"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:MvxBind="Refreshing IsLoading; RefreshCommand ReloadCommand">

<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
android:id="@+id/listsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:MvxItemTemplate="@layout/item_list"
app:MvxBind="ItemsSource Lists; ItemClick ShowListItemsCommand" />

</MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="@dimen/margin_medium"
android:src="@drawable/ic_add_white_24dp"
android:visibility="invisible"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_anchor="@id/main_content_frame"
app:layout_anchorGravity="bottom|right|end" />

</RelativeLayout>

ListsFragment.cs

using Android.OS;
using Android.Views;
using MvvmCross.Droid.Support.V4;
using MvvmCross.Binding.Droid.BindingContext;
using Android.Runtime;
using List.Mobile.Droid.Common;
using MvvmCross.Binding.BindingContext;
using Android.Support.V4.View;
using Android.Widget;
using List.Mobile.Core.ViewModels;
using MvvmCross.Droid.Support.V7.RecyclerView;
using Android.Support.V7.Widget;
using MvvmCross.Droid.Views.Attributes;

namespace List.Mobile.Droid.Views
{
[MvxFragmentPresentation(typeof(MainViewModel), Resource.Id.main_content_frame, true)]
[Register("list.mobile.droid.views.ListsFragment")]
public class ListsFragment : BaseFragment<ListsViewModel>, ActionMode.ICallback
{
...
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
...
}
}
}

基础 fragment .cs

using MvvmCross.Droid.Support.V4;
using List.Mobile.Droid.Common;
using MvvmCross.Core.ViewModels;
using Android.OS;
using Android.Views;
using Android.Support.Design.Widget;
using Android.Support.V7.Widget;
using Android.Support.V7.App;

namespace List.Mobile.Droid.Views
{
public abstract class BaseFragment<T> : MvxFragment<T> where T : MvxViewModel
{
protected AppBarLayout AppBar { get; set; }
protected FloatingActionButton FAB { get; set; }
protected Toolbar Toolbar { get; set; }

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = base.OnCreateView(inflater, container, savedInstanceState);

AppBar = Activity.FindViewById<AppBarLayout>(Resource.Id.main_app_bar);
FAB = Activity.FindViewById<FloatingActionButton>(Resource.Id.fab);
Toolbar = Activity.FindViewById<Toolbar>(Resource.Id.main_tool_bar);
AppCompatActivity parentActivity = ((AppCompatActivity)Activity);
parentActivity.SetSupportActionBar(Toolbar);
parentActivity.SupportActionBar.SetDisplayHomeAsUpEnabled(false);
parentActivity.SupportActionBar.SetHomeButtonEnabled(false);

return view;
}
}
}

问题出在线路上:

parentActivity.SetSupportActionBar(Toolbar);

因为它似乎没有设置操作栏,随后对 parentActivity.SupportActionBar 的调用失败并出现 NullReferenceException。有任何想法吗?当 Appbar 和 Toolbar 是在 Activity 而不是 Fragment 上定义时,请不要这样做。

最佳答案

如果您看不到答案,请休息一下,喝杯咖啡,答案就会浮现。由于 AppBar 和工具栏是在 fragment XML 中定义的,因此我无法调用 Activity.FindViewById() 来对其进行扩充。同样适用于上述代码中的 AppBar 和 FAB。一旦我将代码重构得更接近我正在关注的上述示例(子类 Fragment 提供了 fragment 布局的 ResourceId 并且基类对其进行了扩展),我就能够更改调用:

AppBar = Activity.FindViewById<AppBarLayout>(Resource.Id.main_app_bar);
Fab = Activity.FindViewById<FloatingActionButton>(Resource.Id.fab);
Toolbar = Activity.FindViewById<Toolbar>(Resource.Id.main_tool_bar);

到:

AppBar = FragmentView.FindViewById<AppBarLayout>(Resource.Id.main_app_bar);
Fab = FragmentView.FindViewById<FloatingActionButton>(Resource.Id.fab);
Toolbar = FragmentView.FindViewById<Toolbar>(Resource.Id.main_tool_bar);

哪里:

FragmentView = this.BindingInflate(FragmentResourceId, null);

一切开始正常工作。

关于android - Fragment 上的 SetSupportActionBar 未设置 SupportActionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46857163/

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