DialogFragment参数,空指针异常 [英] DialogFragment argument and nullpointer exception

查看:225
本文介绍了DialogFragment参数,空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类应传递一个参数DialogFragment,但onCreate方法(对话框类的)一个NullPointerException异常里面我的应用程序崩溃。
code的对话片段类部分:

 公共类ConfirmDialog扩展DialogFragment {公共ConfirmDialog(){} ConfirmDialog的newInstance(串f){
    ConfirmDialog D =新ConfirmDialog();    捆绑ARGS =新包();
    args.putString(FILE_NAME,F);
    d.setArguments(参数);    返回D组;
}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    文件= getArguments()的getString(FILE_NAME);
}

我在这行的空指针:

 文件= getArguments()的getString(FILE_NAME);

和我不知道为什么。
我还贴了code调用对话框

 私人无效showConfirmDialog的(字符串文件){
    FragmentManager FM = getSupportFragmentManager();
    ConfirmDialog对话框=新ConfirmDialog();
    Log.i(SHOWFILEACTIVITY文件);
    dialog.newInstance(文件);
    dialog.show(FM,fragment_confirm_dialog);
}

下面的文件字符串没有空,我和

检查

  Log.i(SHOWFILEACTIVITY文件);


解决方案

您正在创建一个 ConfirmDialog 通过构造函数,然后调用的newInstance( ),创建另一个(正确) ConfirmDialog 。然而,你再丢弃,适当的实例。

要解决这个问题:

的newInstance()方法应该是静态的:

 公共静态的newInstance ConfirmDialog(串f){
    ConfirmDialog D =新ConfirmDialog();    捆绑ARGS =新包();
    args.putString(FILE_NAME,F);
    d.setArguments(参数);    返回D组;
}

showConfirmDialog的()应该被改变,因此它使用了的newInstance()方法正确。

 私人无效showConfirmDialog的(字符串文件){
    FragmentManager FM = getSupportFragmentManager();
    Log.i(SHOWFILEACTIVITY文件);    ConfirmDialog对话框= ConfirmDialog.newInstance(文件);
    dialog.show(FM,fragment_confirm_dialog);
}

My class should pass an argument to DialogFragment, but my app crashes inside onCreate method (of dialog class) for a NullPointerException. Dialog fragment class portion of code:

public class ConfirmDialog extends DialogFragment {

public ConfirmDialog() {}

 ConfirmDialog newInstance(String f) {
    ConfirmDialog d = new ConfirmDialog();

    Bundle args = new Bundle();
    args.putString("FILE_NAME", f);
    d.setArguments(args);

    return d;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    file = getArguments().getString("FILE_NAME");
}

I have the nullpointer at this line:

file = getArguments().getString("FILE_NAME");

And i don't know why. I paste also the code calls the dialog

private void showConfirmDialog(String file) {
    FragmentManager fm = getSupportFragmentManager();
    ConfirmDialog dialog = new ConfirmDialog();
    Log.i("SHOWFILEACTIVITY", file);
    dialog.newInstance(file);
    dialog.show(fm, "fragment_confirm_dialog");
}

Here the "file" string is not null, i've check it with

Log.i("SHOWFILEACTIVITY", file);

解决方案

You're creating a ConfirmDialog via the constructor, then calling newInstance(), which creates another (proper) ConfirmDialog. However you then discard that proper instance.

To fix this:

Your newInstance() method should be static:

public static ConfirmDialog newInstance(String f) {
    ConfirmDialog d = new ConfirmDialog();

    Bundle args = new Bundle();
    args.putString("FILE_NAME", f);
    d.setArguments(args);

    return d;
}

And showConfirmDialog() should be changed so it uses the newInstance() method properly.

private void showConfirmDialog(String file) {
    FragmentManager fm = getSupportFragmentManager();
    Log.i("SHOWFILEACTIVITY", file);

    ConfirmDialog dialog = ConfirmDialog.newInstance(file);
    dialog.show(fm, "fragment_confirm_dialog");
}

这篇关于DialogFragment参数,空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆