gpt4 book ai didi

java - 使用 getActivity 设置适配器的 fragment 崩溃

转载 作者:行者123 更新时间:2023-11-30 00:43:28 26 4
gpt4 key购买 nike

我对 fragment 不太了解。当我使用 getActivity() 或 getContext().getApplicationContext() 设置适配器并启动应用程序时,应用程序崩溃。我的测试项目(没有具有相同代码的 fragment )有效。

fragment :

public class RecipesFragment extends Fragment {

public RecipesFragment() {
// Required empty public constructor

}

ArrayList<RecipesData> recipesDatas;
static RecipesAdapter adapter;
ListView listView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view;
view = inflater.inflate(R.layout.fragment_recipes, container, false);


recipesDatas = new ArrayList<>();
listView = (ListView) view.findViewById(R.id.list_recipes);

recipesDatas.add(new RecipesData("test","test","test",30));
recipesDatas.add(new RecipesData("test","test","test",30));
recipesDatas.add(new RecipesData("test","test","test",30));
recipesDatas.add(new RecipesData("test","test","test",30));
recipesDatas.add(new RecipesData("test","test","test",30));

adapter = new RecipesAdapter(recipesDatas, getActivity()) ;

listView.setAdapter(adapter);

return view;
}

}

自定义适配器:

public class RecipesAdapter extends ArrayAdapter<RecipesData> {

private ArrayList<RecipesData> dataSet;
Context mContext;


private static class ViewHolder {
TextView txtName;
TextView txtType;
TextView txtCarbo;
TextView txtTime;
}


public RecipesAdapter(ArrayList<RecipesData> data, Context context) {
super(context, R.layout.row_recipes, data);
this.dataSet = data;
this.mContext = context;
}


@Override
public View getView(int position, View connvertView, ViewGroup parent){

RecipesData recipesData = getItem(position);

ViewHolder viewHolder;

final View result;

if (connvertView == null) {

viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
connvertView = inflater.inflate(R.layout.row_recipes,parent,false);
viewHolder.txtName = (TextView) connvertView.findViewById(R.id.name);
viewHolder.txtType = (TextView) connvertView.findViewById(R.id.type);
viewHolder.txtCarbo = (TextView) connvertView.findViewById(R.id.carbo);
viewHolder.txtTime = (TextView) connvertView.findViewById(R.id.time);

result = connvertView;

connvertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) connvertView.getTag();
result = connvertView;
}

viewHolder.txtName.setText(recipesData.getName());
viewHolder.txtType.setText(recipesData.getType());
viewHolder.txtCarbo.setText(recipesData.getCarbo());
viewHolder.txtTime.setText(recipesData.getTime());

return connvertView;
}


}

日志:

        02-12 22:54:47.710 31001-31001/? E/Zygote: v2
02-12 22:54:47.710 31001-31001/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
02-12 22:54:48.010 31001-31001/de.carolusworld.kitchenhelpster E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.carolusworld.kitchenhelpster, PID: 31001
android.content.res.Resources$NotFoundException: String resource ID #0x1e
at android.content.res.Resources.getText(Resources.java:553)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:4395)
at de.carolusworld.kitchenhelpster.RecipesAdapter.getView(RecipesAdapter.java:66)
at android.widget.AbsListView.obtainView(AbsListView.java:2402)
at android.widget.ListView.makeAndAddView(ListView.java:1891)
at android.widget.ListView.fillDown(ListView.java:713)
at android.widget.ListView.fillFromTop(ListView.java:779)
at android.widget.ListView.layoutChildren(ListView.java:1699)
at android.widget.AbsListView.onLayout(AbsListView.java:2206)
at android.view.View.layout(View.java:16075)
at android.view.ViewGroup.layout(ViewGroup.java:5300)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:16075)
at android.view.ViewGroup.layout(ViewGroup.java:5300)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:16075)
at android.view.ViewGroup.layout(ViewGroup.java:5300)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1091)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:801)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:815)
at android.view.View.layout(View.java:16075)
at android.view.ViewGroup.layout(ViewGroup.java:5300)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1187)
at android.view.View.layout(View.java:16075)
at android.view.ViewGroup.layout(ViewGroup.java:5300)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:16075)
at android.view.ViewGroup.layout(ViewGroup.java:5300)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:16075)
at android.view.ViewGroup.layout(ViewGroup.java:5300)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:16075)
at android.view.ViewGroup.layout(ViewGroup.java:5300)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:16075)
at android.view.ViewGroup.layout(ViewGroup.java:5300)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:16075)
at android.view.ViewGroup.layout(ViewGroup.java:5300)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2119)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1873)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1073)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5988)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Met

最佳答案

问题出在

viewHolder.txtName.setText(recipesData.getName());
viewHolder.txtType.setText(recipesData.getType());
viewHolder.txtCarbo.setText(recipesData.getCarbo());
viewHolder.txtTime.setText(recipesData.getTime());

setText 可以将 int 作为 resource id,因此如果您的任何 get 方法返回 int,你应该使用 String.valueOf(i)

关于java - 使用 getActivity 设置适配器的 fragment 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42193584/

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