TimepickerFragment show()方法无法解析 [英] TimepickerFragment show() method cannot be resolved

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

问题描述

我正在尝试使用对话框片段来选择日期,但是下面的Signigicant_Other类中的timePickerFragment.show(...)方法无法解析.show方法也是由intellisense提出的.我不清楚为什么无法解决,恐怕我看不到一些简单的事情.仅供参考,我遵循了这些教程:

I'm trying to use a dialog fragment to select dates, but the timePickerFragment.show(...) method in the Signigicant_Other class below cannot be resolved. The show method is suggested by the intellisense, also. I'm unclear why It's not resolved, I'm afraid something easy that I'm just not seeing. FYI, I followed these tutorials:

http://developer.android.com/guide/topics/ui/controls/pickers.html http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/

活动调用片段:

package com.mycompany.dudesmyreminders;

import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class Significant_Other extends ActionBarActivity {


    Button birthbutton;
    Button annibutton;
    FragmentManager fm = getSupportFragmentManager();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_significant__other);


        // Locate the button in activity_main.xml


  birthbutton = (Button) findViewById(R.id.SignificanOther_birthButton);
        annibutton = (Button) findViewById(R.id.SignificanOther_annivButton);

    // Capture button clicks
    birthbutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            TimePickerFragment timePickerFragment = new TimePickerFragment();
            // Show DialogFragment
            timePickerFragment.show(fm, "Time Picker"); //THE PROBLEM IS HERE
        }

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_significant__other, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

        return super.onOptionsItemSelected(item);
    }
}

解决方案是下面的更改

具有日期选择器的片段:

Fragment which will have the date picker:

package com.mycompany.dudesmyreminders;

import android.app.Activity;
import android.app.Dialog;
//Solution was to change android.app.DialogFragment; to   android.support.v4.app.DialogFragment;
//import android.app.DialogFragment;
import android.support.v4.app.DialogFragment;
import android.app.TimePickerDialog;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TimePicker;

import java.util.Calendar;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link TimePickerFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener{

    private OnFragmentInteractionListener mListener;

    public TimePickerFragment() {
        // Required empty public constructor
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker


 final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
}



@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p/>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    public void onFragmentInteraction(Uri uri);
}

}

推荐答案

您需要使用

import android.support.v4.app.DialogFragment;

由于您正在使用SupportFragmentManager.祝你好运!

Since you are using the SupportFragmentManager. Good luck!

这篇关于TimepickerFragment show()方法无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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