使用getActivity设置适配器的碎片崩溃

时间:2017-02-12 21:39:05

标签: java android android-fragments fragment custom-adapter

我对片段不了解。 当我使用getActivity()或getContext()。getApplicationContext()设置适配器并启动应用程序时,应用程序崩溃。 我的测试项目(没有相同代码的片段)可以工作。

片段:

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;
}

}

CustomAdapter:

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

1 个答案:

答案 0 :(得分:1)

问题在于

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,您应该使用{{1} }}