总是用一个进度条

总是用一个进度条

本文介绍了DialogFragment:总是用一个进度条,空指针异常。如何解决呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个DialogFragment:

Consider this DialogFragment:

public class RollTriggerDialog extends DialogFragment{

    private ProgressDialog _dialog;
    int _progress;
    public Handler _progressHandler;

    public RollTriggerDialog() {
        // empty
    }

    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        _dialog = new ProgressDialog(getActivity());

        this.setStyle(STYLE_NO_TITLE, getTheme());
        _dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        _dialog.setProgress(0);

        _progressHandler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (_progress >= 100) {
                    _dialog.dismiss();
                } else {
                    _progress++;
                    _dialog.incrementProgressBy(1);
                    _progressHandler.sendEmptyMessageDelayed(0,100);
                }

            }
        };
        //_progressHandler.sendEmptyMessage(0); <- This uncommented would start the progress
        return _dialog;
    }
}

这仅仅是一个水平的进度与处理,一旦处理程序接收一个消息的进度从0到100。

It is just a horizontal progressbar with a handler, once the handler receives one message the progressbar goes from 0 to 100.

我总是得到一个空指针异常如果我想从一个活动触发send​​EmptyMessage由我自己:

I am always getting a Null Pointer Exception if I want to trigger that sendEmptyMessage by myself from an activity:

public class MainActivity extends FragmentActivity {

    private RollTriggerDialog mRollTriggerDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();
        mRollTriggerDialog = new RollTriggerDialog();
        mRollTriggerDialog.show(fm, "addDiceDialog_tag");
        ((RollTriggerDialog)fm.findFragmentByTag("addDiceDialog_tag"))._progressHandler.sendEmptyMessage(0); // <--- NPE HERE!
    }
}

如果sendEmptyMessage的行是dialogFragment和主要活动与NPE行注释掉被注释掉;应用程序运行。什么是错调用?

If the line of sendEmptyMessage is uncommented in the dialogFragment and the line with NPE in the main activity is commented; the app runs. What is wrong with that invocation?

请注意,这是整个code,除清单和布局文件。

Note that this is the whole code, excepting manifest and layout files.

推荐答案

NullPointerException异常出现因为 findFragmentByTag 收益。解决的方法是调用 fm.executePendingTransactions()之前使用 findFragmentByTag 方法来执行片段交易马上( )。

The NullPointerException appears because the findFragmentByTag returns null. The solution is to call fm.executePendingTransactions() before you use the findFragmentByTag method to execute that fragment transaction right away(see this question for more details).

此外,处理程序引用将在那一刻,所以你要在一个初始化它的片段的生命周期方法,例如,的onCreate

Also, the Handler reference will be null at that moment so you'll want to initialize it in one of the fragment's lifecycle methods, for example, onCreate:

public static Handler _progressHandler; // should be made static

//...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    _progressHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (_progress >= 100) {
                _dialog.dismiss();
            } else {
                _progress++;
                _dialog.incrementProgressBy(1);
                _progressHandler.sendEmptyMessageDelayed(0, 100);
            }

        }
    };
}

这篇关于DialogFragment:总是用一个进度条,空指针异常。如何解决呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 13:35