gpt4 book ai didi

Android:从DialogFragment调用Activity的函数

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:20 29 4
gpt4 key购买 nike

我在从 DialogFragment 调用 Activity 的函数时遇到问题。我的 MainActivity 中有一些公共(public)函数,我需要调用这些函数来进行一些在 DialogFragment 中完成的计算。每次我尝试使用 getActivity() 调用函数时。出现“无法解析方法”的问题。

这是我在 MainActivity 中调用 DialogFragment 的方式:

FragmentManager fm = getSupportFragmentManager();
DialogWeekly dialogWeekly = new DialogWeekly();
dialogWeekly.show(getFragmentManager(), "fragment_dialogWeekly");

这就是 DialogFragment 的样子。我在出现上述问题的地方添加了两条注释行:

public class DialogReminder extends DialogFragment implements AdapterView.OnItemSelectedListener {

//--- Static Variables -----------------------------------------------------------------------//
private static final String MY_PREFS = "my_preferences";
private static Activity activity;
private static TimePicker timePicker;
private static View dialogReminderView;

//--- Integer Variables ----------------------------------------------------------------------//
private Integer weekday;

//--- String Variables -----------------------------------------------------------------------//
private String weekdayString;

//--- Other Variables ------------------------------------------------------------------------//
private SharedPreferences sharedPreferences;


/**
* Empty constructor required for DialogFragment
*/
public DialogReminder() { }

/**
* Called when a fragment is first attached to its activity.
* onCreate(Bundle) will be called after this
* @param activity Activity that is attached to this fragment
*/
public void onAttach(Activity activity) {
super.onAttach(activity);
}

//--- Override Functions ---------------------------------------------------------------------//
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_weekly, container);
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getActivity().getSharedPreferences(MY_PREFS, Context.MODE_PRIVATE);

return createAlertDialog();
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Integer selectedItem = parent.getSelectedItemPosition();
weekdayString = parent.getItemAtPosition(pos).toString();
// Here is the problem: savePreferences -> cannot resolve method
getActivity().savePreferences("spinnerSelectionWeekday", String.valueOf(selectedItem));

weekday = selectedItem + 2;
if (weekday == 8) {
weekday = 1;
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}

//--- General Activity Functions -------------------------------------------------------------//

/**
*
* @return
*/
private AlertDialog createAlertDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle(getResources().getString(R.string.optionReminder));
alert.setView(dialogReminderView);

alert.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

alert.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
setReminder();
dialog.cancel();
}
});

setElementsGUI();

return alert.create();
}

/**
*
*/
private void setElementsGUI() {
Spinner spinner = (Spinner) dialogReminderView.findViewById(R.id.reminderWeekdaySpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.reminderSpinnerArray, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(this);
spinner.setAdapter(adapter);
spinner.setSelection(Integer.parseInt(sharedPreferences.getString("spinnerSelectionWeekday", "0")));
}

//--- Button Functions -----------------------------------------------------------------------//

/**
*
*/
private void setReminder() {
// Here is the problem: all functions with getActivity() -> cannot resolve method
getActivity().checkReminder();
getActivity().setWeekdayReminder(weekday);
getActivity(("hour", String.valueOf(timePicker.getCurrentHour()));
getActivity().savePreferences("minute", String.valueOf(timePicker.getCurrentMinute()));

getActivity().checkReminder();

String hour = String.valueOf(getActivity().getHour());
if (hour.length() < 2) {
hour = "0" + hour;
}
String minute = String.valueOf(getActivity().getMinute());
if (minute.length() < 2) {
minute = "0" + minute;
}
String time = hour + ":" + minute;

String message = getResources().getString(R.string.reminderToast, weekdayString, time);
Toast toast = Toast.makeText(getActivity().getApplicationContext(), message, Toast.LENGTH_LONG);
toast.show();
}

最佳答案

虽然 getActivity() 在运行时返回 MainActivity,但编译器必须假定它只是一个 Activity 对象,并且这些方法不存在(因为 Activity 没有这些方法)。因此编译器错误。

您需要做的是将 Activity 转换为 MainActivity 对象,如下所示:

((MainActivity)getActivity()).savePreferences(...

关于Android:从DialogFragment调用Activity的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25818932/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com