不支持方法“ getSupportFragmentManager()”

编程入门 行业动态 更新时间:2024-10-13 20:18:25
本文介绍了不支持方法“ getSupportFragmentManager()”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因此,我正在尝试为以下版本的android 11启用datepicker,因为我正在使用支持库v4。我导入了所有必需的东西:

So i'm trying to enable datepicker for android versions bellow 11. for that i'm using support library v4. I import all the thing necessary:

import android.support.v4.app.*; import android.support.v4.app.FragmentManager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.DialogFragment;

我创建了一个类:

import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import android.app.DatePickerDialog; import android.app.Dialog; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.widget.EditText; public class DatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener { public EditText textField; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(), this, year, month, day); } public EditText getTextField() { return textField; } public void setTextField(EditText textField) { this.textField = textField; } public void onDateSet(DatePicker view, int year, int month, int day) { textField.setText(day+"."+(month+1)+"."+year); } @Override public void onDateSet(android.widget.DatePicker arg0, int arg1, int arg2,int arg3) { textField.setText(arg3+"."+(arg2+1)+"."+arg1); } }

这样,类编译正常。但是问题是当我尝试使用它时。我有一个用于edittext的onclick方法,如下所示:

So class compile ok. But the problem is when i try to use it. I have an onclick method for edittext that looks like that:

public void showDatePicker(View v) { DialogFragment selectDate = (DialogFragment) new DatePicker(); EditText edit=(EditText)v; ((DatePicker) selectDate).setTextField(edit); selectDate.show(getSupportFragmentManager(), "datePicker"); }

但是在最后一行中,我得到了错误:

however in last line i get the error:

The method getSupportFragmentManager() is undefined for the type MainActivity

的方法getSupportFragmentManager()未定义

任何想法如何解决?顺便说一句,我还没有导入

Any ideas how to resolve that? btw i don't have imported anything like

android.app.Fragment;

所以这里不是这样:S

So that is not the case here :S

推荐答案

我的猜测是您的 MainActivity 没有扩展 FragmentActivity !在SupportPackage中,Activity必须从FragmentActivity继承以获取诸如 getSupportedFragmentManager()之类的方法。

My guess is that your MainActivity is not extending FragmentActivity! In the SupportPackage an Activity must inherit from FragmentActivity to get Methods like getSupportedFragmentManager().

编辑:

由于您的Activity是从另一个类继承的,因此您可以尝试实现一个行为这些类中的一种,并合并它们。即在这里,您将找到FragmentActivity的代码: FragmentActivity源

Since your Activity is inheriting from another class, you can try to implement the Behavior of one of these classes and kind of merge them. I.e here you'll find the code for FragmentActivity: FragmentActivity Source

更多推荐

不支持方法“ getSupportFragmentManager()”

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

发布评论

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

>www.elefans.com

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