从警报设置TextView的值(Set the value of a TextView, from an Alert)

编程入门 行业动态 更新时间:2024-10-20 20:49:17
警报设置TextView的值(Set the value of a TextView, from an Alert)

我有一个带有TextView的Fragment(带有3个选项卡的ActionBar Activity) 还有一个打开Alert的按钮,其中有Button和EditText 我想要做的是,当单击Button时,将Fragment的TextView的值设置为Alert的EditText的值。 这是我试图做的(我告诉你的方法是在MAinActivity而不是片段中):

public void showTimePicker(Button but) { final Dialog dialog = new Dialog(this); final TimePicker t; dialog.setContentView(R.layout.alert_time); dialog.setTitle("Heure"); Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); dialogButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TimePicker t = (TimePicker) dialog.findViewById(R.id.time_cri); /* THIS LINE MAKE A NULLPOINTEREXCEPTION */ time.setText(getSelectedTime(t)); /* GET THE TIME FROM TIMEPICKER */ dialog.dismiss(); } }); CriFragment f = (CriFragment) mSectionsPagerAdapter.getItem(2); time = f.getTextView(); /* GETTER OF THE TEXTVIEW I'D LIKE TO SET */ dialog.show(); }

我指出的行生成一个nullPointerException(在onClick()) 谢谢你的阅读!

I've got a Fragment (ActionBar Activity with 3 tabs) with a TextView There is also a Button which open an Alert, in which there is Button and an EditText What i would like to do, is to set the value of the Fragment's TextView to the value of the Alert's EditText, when clicking on the Button. Here is what i tried to do (the method i show you is in the MAinActivity and not in the Fragment) :

public void showTimePicker(Button but) { final Dialog dialog = new Dialog(this); final TimePicker t; dialog.setContentView(R.layout.alert_time); dialog.setTitle("Heure"); Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); dialogButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TimePicker t = (TimePicker) dialog.findViewById(R.id.time_cri); /* THIS LINE MAKE A NULLPOINTEREXCEPTION */ time.setText(getSelectedTime(t)); /* GET THE TIME FROM TIMEPICKER */ dialog.dismiss(); } }); CriFragment f = (CriFragment) mSectionsPagerAdapter.getItem(2); time = f.getTextView(); /* GETTER OF THE TEXTVIEW I'D LIKE TO SET */ dialog.show(); }

The line i pointed out make a nullPointerException (at onClick()) Thank you for reading !

最满意答案

你必须得到TextView“时间”。 因为TextView“时间”未初始化。 您可以使用dialog.findViewById,而不是f.getTextView。

You have to get the TextView "time". Because the TextView "time" isn't initialized. You can use dialog.findViewById, not f.getTextView.

更多推荐

本文发布于:2023-08-06 04:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444545.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   TextView   Alert   Set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!