I'm trying to call a new fragment from a cursor adapter but i get resourceNotFoundException or addView not supported

StackOverflow https://stackoverflow.com/questions/22228839

문제

*I want to be able to call a new fragment when the user clicks on a button which is on every item in the listview but doing it gives me an exception *

  @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();

    }

Hers's how i get parent_view and 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;
    }

Stacktrace:

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)
  • I tried a the following method which gives me another kind of error "java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView" *

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

            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            transaction.replace(parent_view.getId(), frag).addToBackStack(null).commit();
    
도움이 되었습니까?

해결책

OK so i finally found a simple way to solve the question.. I had to pass the parent view through the contructor.. I think the code will explain this better..

From the ProfessorFragment which contains the listview, which in turn uses the custom adater, i get a reference to the parent view and the pass it into adapter's contructor

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

Then in my cursor adapter i get the parent view like this

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

Now with this parent view i can call the new fragment without messing up the view hierarchy

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

This worked fine for me.. thank you all for the support :)

다른 팁

0xFFFFFFFF = -1

That means that you are using invalid resource id.

Check what returns parent_view.getParent()).getId()

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top