【发布时间】:2019-09-06 07:32:14
【问题描述】:
我关注this tutorial 在回收站视图中展示我的物品。我所做的唯一调整是将它们显示在 Fragment 而不是 Activity 中。 在我的应用程序中,我有一个登录屏幕,可在按下按钮时将用户重定向到 Activity。启动屏幕正常工作,但是当按下按钮(→打开活动和片段)时,我得到的只是黑屏。 Logcat 输出也无济于事,因为它不显示任何类型的错误。我得到的只是:
I/ViewConfigCompat:在 ViewConfiguration 上找不到方法 getScaledScrollFactor()
这听起来不仅仅是布局问题而不是代码问题,还是我错了?这个错误可能是由回收站视图中的一些错误引起的吗?
编辑:
经过大量调试后,我至少可以限制调用 ShopFragment 类/视图时发生的错误。将默认 Fragment 设置为其他内容时,它会被渲染。但是一旦我进入 ShopFragment 它就会变成空白并冻结。所以请您好心帮我找出错误:
商店片段:
public class ShopFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
protected View mView;
private OnFragmentInteractionListener mListener;
private ArrayList<Item> items;
public ShopFragment() {
}
public static ShopFragment newInstance(String param1, String param2) {
ShopFragment fragment = new ShopFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//get reference to recyclerView
View view = inflater.inflate(R.layout.fragment_shop, container, false);
this.mView = view;
RecyclerView itemView = mView.findViewById(R.id.rvCategories);
items = Item.ItemList();
ItemAdapter adapter = new ItemAdapter(items);
itemView.setAdapter(adapter);
itemView.setLayoutManager(new LinearLayoutManager(this.getContext()));
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
fragment_shop.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvCategories"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
项目:
public class Item {
private static ArrayList<Item> mItemList;
private String mID, mTitle, mDescription, mProductType, mPictureLink, mCondition, mAvailability, mPrice, mBrand, mGtin, mMpn, mShippingCountry, mService, mShippingCosts, mpubDate;
public Item() {
}
public String getName() {
return mProductType;
}
public static ArrayList<Item> ItemList() {
XMLHandler itemFetcher = new XMLHandler();
itemFetcher.execute();
while (itemFetcher.processing()) {
}
mItemList = itemFetcher.getItems();
Log.i("ITEMS CONTENT", itemFetcher.getItems().toString());
return itemFetcher.getItems();
}
}
item_singleproduct.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:id="@+id/category_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
项目适配器
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private List<Item> mCategories;
ViewHolder vh;
public ItemAdapter(List<Item> categories) {
mCategories = categories;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.item_singleproduct, parent, false);
vh = new ViewHolder(contactView);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Item item = mCategories.get(position);
TextView textView = vh.nameTextView;
textView.setText(item.getmProductType());
}
@Override
public int getItemCount() {
return mCategories.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameTextView;
public ViewHolder(View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.category_name);
}
}
}
【问题讨论】:
-
也添加您的片段代码。
-
检查 R.id.fragmentplace 是否存在于 activity_shop_main_view_screen 的 xml 中或是否存在于 StartupScreen 类的 xml 中。
-
它位于activity_shop_main_view_screen.xml中。
-
注释 ShopFragment 的所有行,只保留 onCreate 和 onCreateView 方法,并通过调试检查您的代码。如果遇到同样的问题,那么 ShopMainViewScreen 中可能存在问题
标签: android android-layout android-fragments android-emulator