【问题标题】:Unexpected NullpointExpection inside a setter [duplicate]设置器中的意外 NullpointExpection [重复]
【发布时间】:2016-06-06 00:47:26
【问题描述】:

我正在尝试做一个简单的操作:单击一个按钮并显示一个自定义 DialogFragment,但我得到一个 NullPointExpection,我不知道为什么。

mAlertDialog.java:

public class mAlertDialog extends DialogFragment {

    TextView title;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_invalid_time, container, false);
        title = (TextView)view.findViewById(R.id.titleText);
        return view;
    }

    public void setTheTitle(String title) {
        this.title.setText(title);
    }
}

显示 mAlertDialog:

mAlertDialog dialog = new mAlertDialog();
dialog.setTheTitle(getActivity().getString(R.string.invalidTimeTitle));
dialog.show(fm, "InvalidTime");

错误信息:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at m.inschool8.Objects.mAlertDialog.setTheTitle(mAlertDialog.java:20)
    at m.inschool8.bSubjects.Fragment_Subjects$55.onClick(Fragment_Subjects.java:2654)
    at android.view.View.performClick(View.java:4780)
    at android.view.View$PerformClick.run(View.java:19866)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

【问题讨论】:

    标签: java android nullpointerexception


    【解决方案1】:

    调用时title 仍然为空

    mAlertDialog dialog = new mAlertDialog();
    dialog.setTheTitle(getActivity().getString(R.string.invalidTimeTitle));
    

    所以你可以做的是

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.dialog_invalid_time, container, false);
            title = (TextView)view.findViewById(R.id.titleText);
            setTheTitle(getActivity().getString(R.string.invalidTimeTitle));
    
            return view;
        }
    

    // 调用它

    mAlertDialog dialog = new mAlertDialog();
    dialog.show(fm, "InvalidTime");
    

    【讨论】:

    • 是的,我可以这样做,但我可能想在未来使用相同的 DialogFragment,但标题不同
    • 那么您可以将titleString 存储为类字段,并从onCreateView 调用其值
    【解决方案2】:

    您的 titlenull,直到显示 DialogFragment 导致 onCreateView 启动。因此更改顺序如下:

    mAlertDialog dialog = new mAlertDialog();
    dialog.show(fm, "InvalidTime");
    dialog.setTheTitle(getActivity().getString(R.string.invalidTimeTitle));
    

    【讨论】:

    • 那没用,同样的错误
    • 异常堆栈是什么?
    • 日志中打印的错误是Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference虽然我不完全明白如何在日志中正确打印,但我做到了catch (Exception e) {Log.e(TAG, e.toString()); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 2021-08-17
    • 2013-05-14
    相关资源
    最近更新 更多