Android 无法解析TimepickerFragment show()方法
我试图使用一个对话框片段来选择日期,但是下面的signigiant_其他类中的timePickerFragment.show(…)方法无法解析。智能感知也提出了显示方法。我不清楚为什么它没有解决,我担心一些简单的事情,我只是没有看到。仅供参考,我遵循了以下教程: 调用片段的活动:Android 无法解析TimepickerFragment show()方法,android,android-fragments,Android,Android Fragments,我试图使用一个对话框片段来选择日期,但是下面的signigiant_其他类中的timePickerFragment.show(…)方法无法解析。智能感知也提出了显示方法。我不清楚为什么它没有解决,我担心一些简单的事情,我只是没有看到。仅供参考,我遵循了以下教程: 调用片段的活动: package com.mycompany.dudesmyreminders; import android.support.v4.app.FragmentManager; import android.supp
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);
}
}
package com.mycompany.dudesmyreminders;
导入android.app.Activity;
导入android.app.Dialog;
//解决方案是改变android.app.DialogFragment;到android.support.v4.app.DialogFragment;
//导入android.app.DialogFragment;
导入android.support.v4.app.DialogFragment;
导入android.app.TimePickerDialog;
导入android.net.Uri;
导入android.os.Bundle;
导入android.app.Fragment;
导入android.text.format.DateFormat;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TimePicker;
导入java.util.Calendar;
/**
*一个简单的{@link Fragment}子类。
*包含此片段的活动必须实现
*{@link TimePickerFragment.OnFragmentInteractionListener}接口
*处理交互事件。
*/
公共类TimePickerFragment扩展了DialogFragment
实现TimePickerDialog.OnTimeSetListener{
私有OnFragmentInteractionListener mListener;
公共时间选择器片段(){
//必需的空公共构造函数
}
@凌驾
创建对话框上的公共对话框(Bundle savedInstanceState){
//使用当前时间作为选择器的默认值
最终日历c=Calendar.getInstance();
int hour=c.get(日历小时);
int minute=c.get(Calendar.minute);
//创建TimePickerDialog的新实例并返回它
返回新的TimePickerDialog(getActivity(),this,hour,minute,
DateFormat.is24HourFormat(getActivity());
}
时间集上的公共void(时间选择器视图,整数小时日,整数分钟){
//用用户选择的时间做一些事情
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
试一试{
mListener=(OnFragmentInteractionListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实现OnFragmentInteractionListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*
*有关更多信息,请参阅Android培训课程。
*/
FragmentInteractionListener上的公共接口{
//TODO:更新参数类型和名称
公共void onFragmentInteraction(Uri);
}
}
您需要使用
import android.support.v4.app.DialogFragment;
因为您正在使用SupportFragmentManager。祝你好运 我曾尝试将其添加到其他文件中,但它将其标记为未使用。support.v4是否用于旧版本的android?好的。在DatePickerFragment上尝试了它,这就清除了错误。谢谢关于support.v4相对于标准版的好处,有什么建议吗?我的问题
FragmentManager
和DialogFragment
中添加的解决方案要求API级别11或更高如果您计划支持低于此级别的API版本,则应使用支持库。