gpt4 book ai didi

android - TimepickerFragment show() 方法无法解析

转载 作者:行者123 更新时间:2023-11-29 17:40:15 25 4
gpt4 key购买 nike

我正在尝试使用对话框 fragment 来选择日期,但无法解析下面 Signigicant_Other 类中的 timePickerFragment.show(...) 方法。智能感知也建议使用 show 方法。我不清楚为什么它没有解决,恐怕我只是没有看到一些简单的事情。仅供引用,我遵循了这些教程:

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

调用 fragment 的 Activity :

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 :

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。祝你好运!

关于android - TimepickerFragment show() 方法无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29054055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com