本文介绍了不能做对话管理器后的东西(弹出)dissapear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在以后的片段弹出dissapear做一些事情。但是,我得到的错误。请帮助我..

I want to doing something after pop up dissapear in fragment. But, I get error. Please, help me..

Main.java

Main.java

public class Main extends Fragment implements SendStringEvent{
    Button addButton;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.teacher_category, container, false); 
        addButton = (Button) rootView.findViewById(R.id.add_category_button);
        addButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                TEACHER_AddCategory teacherAddCategory = new TEACHER_AddCategory();
                teacherAddCategory.show(getFragmentManager(), "TEACHER_AddCategory");
            }
        });
       return rootView;
    }

    //This method from interface SendStringEvent
    @Override
    public void eventAfterMessageClosed(String message) {
        //I get error when executing this
        addButton.setText(message);
    }
}

TEACHER_AddCategory.java (POP UP)

public class TEACHER_AddCategory extends DialogFragment {
    Button OKButton;
    EditText editText;
    private SendStringEvent eventMessageClosed;
    Fragment fragment = new Main();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(seamolec.ujianonline.R.layout.teacher_add_category, container);
        OKButton = (Button) view.findViewById(seamolec.ujianonline.R.id.ok_button);
        try {
            eventMessageClosed = (SendStringEvent) fragment;
        }
        catch(ClassCastException e) {
            e.printStackTrace();
        }
        OKButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                    eventMessageClosed.eventAfterMessageClosed("test");
                    dismiss();
                }
            }
        });
        return view;
    }

}

这是接口SendStringEvent.java

This is interface SendStringEvent.java

 public interface SendStringEvent {
    void eventAfterMessageClosed(String message);
}

我遇到错误时弹出dissapear和系统正在执行的eventAfterMessageClosed()

登录猫:

Log Cat :

推荐答案

它的发生,因为你正试图从调用一个新的实例,甚至没有加载所以观点尚未创建方法。

Its happening because you are trying to call the method from a new instance which is not even loaded so view hasn't been created yet.

修改

try {
        eventMessageClosed = (SendStringEvent) fragment;
    }
    catch(ClassCastException e) {
        e.printStackTrace();
    }

Fragment fr = getFragmentManager().findFragmentById(R.id.where_fragment_is_loaded) // Change as per you layout
if(fr != null && fr instanceOf SendStringEvent)
    eventMessageClosed = (SendStringEvent) fr;

和变化

eventMessageClosed.eventAfterMessageClosed("test");

if(eventMessageClosed != null)
    eventMessageClosed.eventAfterMessageClosed("test");

注意: R.id.where_fragment_is_loaded 的象征。使用你在哪里加载的片段的ID。还可以使用 getSupportFragmentManager()如果您正在使用支持库。

NOTE: R.id.where_fragment_is_loaded is indicative. Use the id where you are loading your fragment Main. Also use getSupportFragmentManager() if you are using support library.

这篇关于不能做对话管理器后的东西(弹出)dissapear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 21:38