gpt4 book ai didi

java - findViewById() 可能会为 HomeFragment.java 中的 getView() 产生 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 19:15:21 25 4
gpt4 key购买 nike

公共(public)类 HomeFragment 扩展 Fragment 实现 HomeView{

private HomeViewModel homeViewModel;
public static final String EXTRA_CATEGORY = "category";
public static final String EXTRA_POSITION = "position";
public static final String EXTRA_DETAIL = "detail";

@BindView(R.id.viewPagerHeader)
ViewPager viewPagerMeal;
@BindView(R.id.recyclerCategory)
RecyclerView recyclerViewCategory;

HomePresenter presenter;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
super.onCreate(savedInstanceState);
ButterKnife.bind(getActivity());

presenter = new HomePresenter(this);
presenter.getMeals();
presenter.getCategories();

return root;
}
@Override
public void showLoading() {
getView().findViewById(R.id.shimmerMeal).setVisibility(View.VISIBLE);
getView().findViewById(R.id.shimmerCategory).setVisibility(View.VISIBLE);
}

@Override
public void hideLoading() {
getView().findViewById(R.id.shimmerMeal).setVisibility(View.GONE);
getView().findViewById(R.id.shimmerCategory).setVisibility(View.GONE);
}

@Override
public void setMeal(List<Meals.Meal> meal) {
ViewPagerHeaderAdapter headerAdapter = new ViewPagerHeaderAdapter(meal, getActivity());
viewPagerMeal.setAdapter(headerAdapter);
viewPagerMeal.setPadding(20, 0, 150, 0);
headerAdapter.notifyDataSetChanged();

headerAdapter.setOnItemClickListener((view, position) -> {
TextView mealName = view.findViewById(R.id.mealName);
Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class);
intent.putExtra(EXTRA_DETAIL,mealName.getText().toString());
startActivity(intent);
});
}

@Override
public void setCategory(List<Categories.Category> category) {
RecyclerViewHomeAdapter homeAdapter = new RecyclerViewHomeAdapter(category, getActivity());
recyclerViewCategory.setAdapter(homeAdapter);
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3,
GridLayoutManager.VERTICAL, false);
recyclerViewCategory.setLayoutManager(layoutManager);
recyclerViewCategory.setNestedScrollingEnabled(true);
homeAdapter.notifyDataSetChanged();

homeAdapter.setOnItemClickListener((view, position) -> {
Intent intent = new Intent(getActivity(), CategoryActivity.class);
intent.putExtra(EXTRA_CATEGORY, (Serializable) category);
intent.putExtra(EXTRA_POSITION, position);
startActivity(intent);
});
}

@Override
public void onErrorLoading(String message) {
Utils.showDialogMessage(getActivity(), "Title", message);
}

}

公共(public)类 HomeViewModel 扩展 ViewModel {

private MutableLiveData<String> mText;

public HomeViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is home fragment");
}



public LiveData<String> getText() {
return mText;
}

}

fragment_home.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment"
android:fitsSystemWindows="true">

<android.support.v4.widget.NestedScrollView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground">
<RelativeLayout
android:id="@+id/headerView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp">
<View
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="@color/colorPrimary" />

<TextView
android:id="@+id/title"
android:textColor="@color/colorWhite"
android:text="@string/app_name"
android:textStyle="bold"
android:textSize="30sp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<android.support.v7.widget.CardView
android:id="@+id/cardSearch"
android:layout_below="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="15dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
app:cardCornerRadius="10dp"
app:cardElevation="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="10dp"
android:text="@string/search_your_recipes"
android:textColor="@color/colorPrimaryLight"
android:gravity="center_vertical"
android:drawableLeft="@drawable/ic_search_primary_light"
android:drawableStart="@drawable/ic_search_primary_light"
android:drawablePadding="10dp"/>
</android.support.v7.widget.CardView>

<android.support.v4.view.ViewPager
android:id="@+id/viewPagerHeader"
android:foregroundGravity="center"
android:overScrollMode="never"
android:clipToPadding="false"
android:layout_below="@id/cardSearch"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="150dp" />

<include android:id="@+id/shimmerMeal"
layout="@layout/item_view_pager_header_shimmer" />

<TextView
android:id="@+id/titleCategory"
android:text="@string/meal_categories"
android:textSize="19sp"
android:textColor="@color/colorPrimary"
android:textStyle="bold"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/viewPagerHeader"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerCategory"
android:scrollbars="none"
android:layout_marginTop="10dp"
android:clipToPadding="false"
android:layout_below="@id/titleCategory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="50dp" />

<include android:id="@+id/shimmerCategory"
layout="@layout/item_recycler_category_shimmer" />

<TextView
android:padding="10dp"
android:gravity="center"
android:textColor="@color/colorWhite"
android:text="@string/app_name"
android:background="@color/colorPrimaryLight"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>

</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

这是 Logcat 文件:

2019-12-20 18:04:26.459 4910-4910/com.vikaskonaparthi.origin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vikaskonaparthi.origin, PID: 4910
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.vikaskonaparthi.origin.ui.home.HomeFragment.showLoading(HomeFragment.java:63)
at com.vikaskonaparthi.origin.ui.home.HomePresenter.getMeals(HomePresenter.java:29)
at com.vikaskonaparthi.origin.ui.home.HomeFragment.onCreateView(HomeFragment.java:56)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2372)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:874)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:6729)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

HomeFragmentActivity 中的微光出现错误如何解决此问题?showloading() 应该保留在 FragmentViewModel 中吗?因为我已经将 Activity 转换为此处的 fragment 。

最佳答案

问题不在于闪光。

问题出在 getView().findViewById(R.id.shimmerMeal).setVisibility(View.VISIBLE); 中的 getView()一片空白。

如果您在 fragment 中拥有微光 View ,则需要在调用 fragment onViewCreated() 方法后调用 showLoading()

关于java - findViewById() 可能会为 HomeFragment.java 中的 getView() 产生 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59425334/

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