方法'getSupportFragmentManager()'是未排除的 [英] The method 'getSupportFragmentManager()' is unsuported

查看:95
本文介绍了方法'getSupportFragmentManager()'是未排除的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图启用在下面的Android版本的日期戳。因为我使用支持库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

任何想法如何解决? btw我没有导入任何东西,如

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

android.app.Fragment;

所以不是这样的: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()'是未排除的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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