NullPointerException异常而在AlertDialog改变TextView的文本 [英] NullPointerException while changing textView text in an AlertDialog

查看:457
本文介绍了NullPointerException异常而在AlertDialog改变TextView的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:


  • 我只是想创建一个customDialog与特定的布局和添加的内容。

  • 它是一个DialogFragment

code:

  TextView的文本;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
 super.onCreate(savedInstanceState); 文字=(TextView的)getView()findViewById(R.id.lorem)。
 text.setText(测试);
}
@覆盖
公共对话框onCreateDialog(捆绑savedInstanceState){ AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
 LayoutInflater吹气= getActivity()getLayoutInflater()。
 builder.setView(inflater.inflate(R.layout.custom_dialog,NULL));
 返回builder.create();
}

问题:


  • 对话框的工作,但后,我开始添加的行的onCreate我得到一个NullPointerException异常错误。我希望有人能帮助我。


感谢@Raghunandan的答案+解释,这里的工作code:

  @覆盖
    公共对话框onCreateDialog(捆绑savedInstanceState){     AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
     LayoutInflater吹气= getActivity()getLayoutInflater()。
     查看查看= inflater.inflate(R.layout.custom_dialog_style,NULL);
     的TextView =(TextView中)view.findViewById(R.id.your_textview);
     textView.setText(测试);
     builder.setView(视图);
     返回builder.create();
    }


解决方案

起初我还以为它是一个活动现在我看到它是一个 DialogFragment

我猜测认为属于 custom_dialog.xml

 查看= inflater.inflate(R.layout.custom_dialog,NULL);
文字=(TextView的)view.findViewById(R.id.lorem);
builder.setView(视图);

因此​​,使用视图对象初始化的TextView

文本=(TextView的)getView()findViewById(R.id.lorem); 这里 getView()返回null。

您在呼唤的setText 对空导致 NullPointerException异常

Background:

  • I just want to create a customDialog with a specific layout and add the content.
  • Its a DialogFragment

Code:

TextView text;

@Override
public void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);

 text = (TextView)getView().findViewById(R.id.lorem);
 text.setText("Test");
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState){

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
 LayoutInflater inflater = getActivity().getLayoutInflater();
 builder.setView(inflater.inflate(R.layout.custom_dialog, null));
 return builder.create();
}

Problem:

  • The Dialog worked, but after I started to add the the lines in "onCreate" I got a NullPointerException Error. I hope someone can help me.

Thanks @Raghunandan for the Answer + explanation and here is the working code:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     LayoutInflater inflater = getActivity().getLayoutInflater();
     View view = inflater.inflate(R.layout.custom_dialog_style, null);
     textView = (TextView)view.findViewById(R.id.your_textview);
     textView.setText("Test");
     builder.setView(view);
     return builder.create();
    }

解决方案

At first i thought its a Activity now i see its a DialogFragment.

I am guessing the view belongs to custom_dialog.xml

View view = inflater.inflate(R.layout.custom_dialog, null);
text = (TextView)view.findViewById(R.id.lorem); 
builder.setView(view);

So use the view object to initialize TextView.

text = (TextView)getView().findViewById(R.id.lorem); here getView() returns null.

You are calling setText on null leading to NullPointerException.

这篇关于NullPointerException异常而在AlertDialog改变TextView的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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