当前位置: 首页 > 面试题库 >

无法解析方法'show(android.support.v4.app.FragmentManager,java.lang.String)

贝自怡
2023-03-14
问题内容

由于某些原因,当我尝试显示对话框时,我从dialog.show(fm,DIALOG_DATE);中得到了一个错误。说
无法解析方法’show(android.support.v4.app.FragmentManager,java.lang.String)’

为什么无法解决该方法?

mDateButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            DatePickerFragment dialog = new DatePickerFragment();
            dialog.show(fm, DIALOG_DATE);
        }
    });

这是我课程的其余部分:

package com.bignerdranch.android.criminalintent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.Editable; 
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

import java.util.UUID;


public class CrimeFragment extends Fragment {
//key for the extra
public static final String EXTRA_CRIME_ID = "com.bignerdranch.android.criminalintent.crime_id";

private static final String DIALOG_DATE = "date";

//holds crime
private Crime mCrime;

//widgets
private EditText mTitleField;
private Button mDateButton;
private CheckBox mSolvedCheckBox;

//at start of build
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //get crime from crime class

    /*Intents
    *There are two ways a fragment can access data in its activity's intent:
    * an easy direct shortcut
    * or a complex flexible implementation
    * First try out the shortcut
    * in the shortcut, CrimeFragment will access CrimeActivity's intent directly
     */

    //retrieve the extra from CrimeActivity's intent and use it to fetch the Crime
    //UUID crimeId = (UUID)getActivity().getIntent().getSerializableExtra   (EXTRA_CRIME_ID); //shortcut removed in chapter 10 and "should feel warm and fuzzy inside for maintaining CrimeFragments Independence"
    //
    UUID crimeId = (UUID)getArguments().getSerializable(EXTRA_CRIME_ID);
    //CrimeLab.get() requires a context object, so CrimeFragment passes the CrimeActivity
    mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);

}

//Create the view and inflate the layout
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for crime Fragment
    //pass false because view will be added in the activitys code
    View v = inflater.inflate(R.layout.fragment_crime, container, false);

    //gets crime_title from fragment_crime.xml
    mTitleField = (EditText)v.findViewById(R.id.crime_title);
    mTitleField.setText(mCrime.getTitle());
    mTitleField.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //not used
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
              mCrime.setTitle(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
            //also not used
        }
    });


    //find date button from fragment_crime
    mDateButton = (Button)v.findViewById(R.id.crime_date);
    //set mDateButton text to current date and time
    mDateButton.setText(mCrime.getDate().toString());
    //disable button for now...enabled in chapter 12
   // mDateButton.setEnabled(false);


    mDateButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            DatePickerFragment dialog = new DatePickerFragment();
            dialog.show(fm, DIALOG_DATE);
        }
    });




    //find solved checkbox from fragment_crime
    mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
    mSolvedCheckBox.setChecked(mCrime.isSolved());
    //user clicks solved check box
    mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
            //set the crime's solved property
            mCrime.setSolved(isChecked);
        }
    });
    //returns the view
    return v;
}

/*The downside to direct retrieval
*can not encapsulate fragment
* CrimeFragment is no longer a reusable building block because it expects that it will always be hosted by an activity whose intent defines extra named "EXTRA_CRIME_ID"
* CrimeFragment cannot be used with just any activity
*
 */

/*Fragment Arguments
*A better solution is to stash the mCrimeId someplace that belongs to CrimeFragment rather than keeping it in CrimeActivity's personal space
* this someplace can be an arguments bundle
* Every fragment instance can have a Bundle object attached to it
* bundle contains key value pairs that work just like the intent extras of an activity
* Pg. 195
 */

/*attaching arguments to a fragment
*Attaching args to frags must be done after the frag is created but before it is added to the activity
* To hit this window use a static class called newInstance()
* This method creates the fragment instance and bundles up and sets its arguments
 */
//for attaching arguments to a fragment
public static CrimeFragment newInstance(UUID crimeId){
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CRIME_ID, crimeId);

    CrimeFragment fragment = new CrimeFragment();
    fragment.setArguments(args);

    //pass UUID from extra
    return fragment;
}

}


问题答案:

为了解决这个问题,如果您使用的是 android.app.DialogFragment ,请使用
getFragmentManager()


mDateButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            FragmentManager fm = getActivity().getFragmentManager();
            DatePickerFragment dialog = new DatePickerFragment();
            dialog.show(fm, DIALOG_DATE);
        }
    });

要使用
getSupportFragmentManager()
,必须从 android.support.v4.app.DialogFragment 扩展。

检查您的导入:

import android.support.v4.app.DialogFragment;


 类似资料:
  • 问题内容: 我正在使用Fragments for tablet创建一个应用程序。到目前为止,我已经在左侧创建了一些按钮,并且在单击按钮时出现了碎片。 但是我在MainActivity.java文件中遇到错误“类型不匹配错误:无法从转换为”,这是我无法实现它的主要原因。我已经导入了。 我该如何解决我的问题? 我的MainActivity.java代码 问题答案: 您无需在代码中使用- 详情请参见此处

  • 我用Android Studio而不是Eclipse。我安装它,然后启动一个全新的项目,并按照向导操作。我没有添加自己的代码。 然后,我右键点击创建一个新的组件,一个新的片段: 然后选择一个新片段: 当我这样做的时候,我就会看到编译错误: ...所以我开始搜索,发现我需要安装和引用支持库4,我确实需要。当我检查build.gradle(对我来说,这是从Eclipse新来的)时,我看到: 但我把它改

  • 问题内容: 错误使我发疯。 在我的应用中,我在MainActivity中创建了3个导航抽屉项,和。现在,我想添加两个与的项目。 MainActivity //用于导航抽屉 TabsFragmentPagerAdapter.java ViewView.java 问题: 错误:(49,81)错误:不兼容的类型:android.app.FragmentManager无法转换为android.suppor

  • 我正在通过Android Studio中的一个应用程序工作,该应用程序使用学校意图传递数据。我已经创建了传递数据的对象,并启动了,但是我不断收到一个警告,说我的方法无法解析。有什么想法吗?提前谢了。

  • 正如文件所述: Android O允许您通过在res/字体/文件夹中添加字体文件来捆绑字体作为资源。 结果: 您可以使用getFont(int)方法检索字体,其中需要传递要检索的字体的资源标识符。此方法返回Typeface对象。这将对字体的第一个重量或样式变体(如果是字体系列)进行编码。然后可以使用字体。create(typeface,style)方法来检索特定样式。 注意:TextView已经为

  • > 在菜单项和添加导航头之间导航的代码由一个方法组成。 由于作者没有提到在哪里粘贴这段代码,我粘贴在我的文件中 在菜单项之间导航和添加导航标题之间的代码是否由我粘贴在正确的位置? 在方法selectDrawerItem(MenuItem MenuItem)中有一条注释,创建一个新片段,并根据位置指定要显示的行星,作者是否希望我在这里添加一些内容。