TimepickerFragment show()方法无法解析

时间:2015-03-14 20:51:49

标签: android android-fragments

我尝试使用对话框片段来选择日期,但无法解析下面Signigicant_Other类中的timePickerFragment.show(...)方法。 showllisense也提出了show方法。我不清楚为什么它没有得到解决,我害怕一些我没有看到的容易的东西。仅供参考,我遵循了以下教程:

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);
    }
}

编辑:解决方案是以下指示的变化

具有日期选择器的片段:

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);
}

}

1 个答案:

答案 0 :(得分:3)

您需要使用

import android.support.v4.app.DialogFragment;

因为您使用的是SupportFragmentManager。祝好运!