我试图从游标适配器调用一个新的片段,但我得到resourceNotFoundException或不支持addView

时间:2014-03-06 15:29:00

标签: android android-fragments android-view android-cursoradapter

* 我希望能够在用户点击列表视图中每个项目上的按钮时调用新片段,但这样做会给我一个异常*

  @Override
    public void bindView(View convertView, Context context, Cursor cursor) {

        final ViewHolder view_handler = new ViewHolder();

        ...
        ...


        view_handler.update_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                FragmentManager fm = frag_context.getSupportFragmentManager(); 
        AddProfFragment frag = new AddProfFragment();
        FragmentTransaction transaction = fm.beginTransaction();

        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.replace(((ViewGroup) parent_view.getParent()).getId(), frag).addToBackStack(null).commit();

    }

她是如何获得parent_view和frag_context

public ProfessorCardAdapter(Context context, Cursor c, boolean autoRequery) {
    super(context, c, autoRequery);
    appContext = context;
    dataSource = new ProfessorDataSource(context);
    frag_context = (FragmentActivity) context;
    this.c = c;
}

 @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // when the view will be created for first time,
        // we need to tell the adapters, how each item will look
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.from(context).inflate(R.layout.professors_list_item, parent, false);
        parent_view = parent;
        return retView;
    }

堆栈跟踪:

android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff
            at android.content.res.Resources.getResourceName(Resources.java:1773)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
  • 我尝试了以下方法,它给了我另一种错误" java.lang.UnsupportedOperationException:AdapterView"不支持addView(View)。 *

    FragmentManager fm = frag_context.getSupportFragmentManager();             AddProfFragment frag = new AddProfFragment();             FragmentTransaction事务= fm.beginTransaction();

            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            transaction.replace(parent_view.getId(), frag).addToBackStack(null).commit();
    

2 个答案:

答案 0 :(得分:1)

好的,所以我终于找到了解决问题的简单方法..我必须通过构造函数传递父视图..我认为代码会更好地解释这个......

来自包含listview的ProfessorFragment,后者又使用自定义adater,我获得对父视图的引用并将其传递给适配器的构造函数

 new android.os.Handler().post(new Runnable() {
            @Override
            public void run() {
                cursor = dataSource.getAllProfessors();
                /*Get parent view*/
                parent_view = ((ViewGroup) getView().getParent());
                adapter = new ProfessorCardAdapter(getActivity(), cursor, true, parent_view);
                professors_listview.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        });

然后在我的游标适配器中,我得到像这样的父视图

public ProfessorCardAdapter(Context context, Cursor c, boolean autoRequery, ViewGroup p_view) {
    super(context, c, autoRequery);
    appContext = context;
    dataSource = new ProfessorDataSource(context);
    frag_context = (FragmentActivity) context;
    parent_view = p_view;
    this.c = c;
}

现在有了这个父视图,我可以调用新片段而不会弄乱视图层次结构

FragmentManager fm = frag_context.getSupportFragmentManager();
UpdateProfessorFragment frag = new UpdateProfessorFragment();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(parent_view.getId() , frag).addToBackStack(null).commit();

这对我来说很好..谢谢大家的支持:))

答案 1 :(得分:0)

0xFFFFFFFF = -1

这意味着您使用了无效的资源ID。

检查返回 parent_view.getParent())的内容.getId()