我有一个带有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.
更多推荐
发布评论