gpt4 book ai didi

java - 将 RecyclerView 代码直接从 Activity 移动到 Fragment,这些错误是什么意思?

转载 作者:行者123 更新时间:2023-12-01 20:53:47 25 4
gpt4 key购买 nike

我确实在MainActivity中运行了以下代码;但决定将其移至如下所示的 Fragment 中。我已经解决了 IDE 中显示的所有错误,但仍在努力调试启动时附加的 logcat。

fragment :

package mackinnoninc.packmule;


import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


//Created on 09/03/2017.
public class FragmentInventory extends Fragment implements ListAdapter.Listener {

RecyclerView recyclerView;
DbHelper dbHelper;
ListAdapter adapter;
FloatingActionButton fab;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_inventory, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.rv_contactlist);
fab = (FloatingActionButton) view.findViewById(R.id.fab);

dbHelper = DbHelper.getInstance(getActivity().getApplicationContext());


adapter = new ListAdapter(getActivity().getApplicationContext(), dbHelper.getAllUser());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));

ItemTouchHelper.Callback callback =
new SimpleItemTouchHelperCallback(adapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(recyclerView);

totalQuantity();


fabHideShow();
return view;
}

@Override
public void nameToChnge(String name) {
dbHelper.deleteRow(name);

adapter = new ListAdapter(getActivity().getApplicationContext(), dbHelper.getAllUser());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
}


public void totalQuantity() {
/** Sums the Quantities, not currently doing anything else, but I will need it in the future
* */
Integer totalSum = dbHelper.getTotal();
}

public void introItem() {
dbHelper = DbHelper.getInstance(getActivity().getApplicationContext());
UserData userData = new UserData();
SharedPreferences prefs = this.getActivity().getSharedPreferences("introItem", getActivity().getApplicationContext().MODE_PRIVATE);
Integer restoredText = prefs.getInt("introItemCheck", 0);

if (restoredText == 0){
userData.sort_id = 1;
userData.name = "Adding Items";
userData.quantity = "1";
userData.description = "To add an item, click the add button below. To edit an item, click it." ;
dbHelper.insertUserDetail(userData);

userData.sort_id = 2;
userData.name = "Absorbing Shield";
userData.quantity = "1";
userData.description = "+1 heavy steel shield; appears to absorb light; can disintegrate an object on touch; " +
"2 day cool down. Worth 25,000gp. This is an example.";
dbHelper.insertUserDetail(userData);

SharedPreferences.Editor editor = this.getActivity().getSharedPreferences("introItem", getActivity().getApplicationContext().MODE_PRIVATE).edit();
editor.putInt("introItemCheck", 1);
editor.apply();
}
}


public void fabHideShow () {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy){
if (dy > 0)
fab.hide();
else if (dy < 0)
fab.show();
}
});
}

}

fragment_inventory.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"fra
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/deepBlue">


<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rv_contactlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollbars="vertical" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:onClick="addItem"
android:src="@drawable/ic_add_circle_outline_black_24dp"
app:fabSize="normal"
app:layout_anchor="@id/rv_contactlist"
app:layout_anchorGravity="bottom|right|end" />

</RelativeLayout>

Logcat:

03-09 17:59:40.071 17173-17173/company.appname E/AndroidRuntime: FATAL EXCEPTION: main
Process: company.appname, PID: 17173
java.lang.RuntimeException: Unable to start activity ComponentInfo{company.appname/company.appname.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at company.appname.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at company.appname.MainActivity.onCreate(MainActivity.java:29) 
at android.app.Activity.performCreate(Activity.java:6259) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to company.appname.ListAdapter$Listener
at company.appname.ListAdapter.<init>(ListAdapter.java:36)
at company.appname.FragmentInventory.onCreateView(FragmentInventory.java:35)
at android.app.Fragment.performCreateView(Fragment.java:2220)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:949)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1228)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2203)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:5567)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at company.appname.MainActivity.onCreate(MainActivity.java:29) 
at android.app.Activity.performCreate(Activity.java:6259) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

列表适配器:

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ItemViewHolder>
implements ItemTouchHelperAdapter {

private static final String TAG = "ListAdapter";
Context context;
private List<UserData> dataList = new ArrayList<>();
LayoutInflater inflater;
Listener listener;
DbHelper dbHelper;



public interface Listener {
void nameToChnge(String name);
}

public ListAdapter(Context context, List<UserData> dataList1) {
this.context = context;
this.dataList = dataList1;
this.listener= (Listener) context;
inflater = LayoutInflater.from(context);
}


@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View convertView = inflater.inflate(R.layout.recylerview_one, parent, false);
ItemViewHolder viewHolder = new ItemViewHolder(convertView);
return viewHolder;
}

@Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
holder.tv_name.setText(dataList.get(position).name);
holder.tv_quantity.setText(dataList.get(position).quantity);
holder.tv_description.setText(dataList.get(position).description + " ");

holder.relLayout.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String s = dataList.get(position).id;
Integer stringo = Integer.parseInt(s);
Intent intent = new Intent(context, ItemEditActivity.class);
intent.putExtra("ItemNumber", stringo);
context.startActivity(intent);
}
});
}

@Override
public int getItemCount() {
return dataList.size();
}

class ItemViewHolder extends RecyclerView.ViewHolder {
TextView tv_name, tv_quantity, tv_description;
RelativeLayout relLayout;


public ItemViewHolder(View itemView) {
super(itemView);
tv_name = (TextView) itemView.findViewById(R.id.nameDisplay);
tv_quantity = (TextView) itemView.findViewById(R.id.quantityDisplay);
tv_description = (TextView) itemView.findViewById(R.id.descriptionDisplay);
relLayout = (RelativeLayout) itemView.findViewById(R.id.relLayout);
}
}


@Override
public void onItemDismiss(final int position) {
dataList.remove(position);
dbHelper = DbHelper.getInstance(context);
dbHelper.deleteRowItem(position + 1);
this.notifyItemRemoved(position);
}

//Collections.swap(dataList, fromPosition, toPosition);
//notifyItemMoved(fromPosition, toPosition);


@Override
public boolean onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(dataList, i, i + 1);

}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(dataList, i, i - 1);
}
}
this.notifyItemMoved(fromPosition, toPosition);
Log.d("fg", fromPosition + " - " + toPosition);
return true;
}
}

最佳答案

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to company.appname.ListAdapter$Listener at company.appname.ListAdapter.(ListAdapter.java:36)

似乎您正在将 ListAdapter 构造函数的第一个参数转换为 ListAdapter 类中的 company.appname.ListAdapter$Listener 的某个位置.

当您使用 Activity 实现该接口(interface)时,一切工作正常,但在迁移到 Fragment 后,您应该分别传递 ContextListener

编辑

只需将 3 参数构造函数添加到您的 ListAdapter 即可:

public ListAdapter(Context context, Listener listener, List<UserData> dataList1) {
this.context = context;
this.dataList = dataList1;
this.listener = listener;
inflater = LayoutInflater.from(context);
}

关于java - 将 RecyclerView 代码直接从 Activity 移动到 Fragment,这些错误是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42702828/

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