show。(getFragmentManager)with extends Fragment not FragmentActivity

时间:2014-11-08 17:01:41

标签: android android-fragments android-dialogfragment

我偶然发现了一个需要帮助的问题。 我有一个带有ViewPager的标签布局,我在其中定义了三个不同的标签。 FragmentTab1,FragmentTab2,FragmentTab3。 在我的FragmentTab3中,我想使用TimePicker。为此,我定义了一个TimePickerFragment.java类;如下所示:

public class TimePickerFragment extends DialogFragment implements 
   TimePickerDialog.OnTimeSetListener {

private TimePickedListener mListener;

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

@Override
public void onAttach(Activity activity)
{
    // when the fragment is initially shown (i.e. attached to the activity), cast the activity to the callback interface type
    super.onAttach(activity);
    try
    {
        mListener = (TimePickedListener) activity;
    }
    catch (ClassCastException e)
    {
        throw new ClassCastException(activity.toString() + " must implement " + TimePickedListener.class.getName());
    }
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
    // when the time is selected, send it to the activity via its callback interface method
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);

    mListener.onTimePicked(c);
}

public static interface TimePickedListener
{
    public void onTimePicked(Calendar time);

    View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState);
}
}

奥基。所以在我的FragmentTab3.java文件中,我尝试使用以下代码:

 import java.util.Calendar;

import android.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.format.DateFormat;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import com.example.app.TimePickerFragment.TimePickedListener;

public class FragmentTab3 extends Fragment implements TimePickedListener {

private TextView mPickedTimeText;
private Button mPickTimeButton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Get the view from fragmenttab3.xml
    View view = inflater.inflate(R.layout.fragmenttab3, container, false);

    //Skapar en ListView och kopplar till listview i xml
    GridView gridView = (GridView) view.findViewById(R.id.gridView1);

    //Arrayen som listan kommer ta värden ifrån
    String [] items = new String[]{"Me", "Testing", "Grid", "View"};
    ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items); 

    gridView.setAdapter(adapter);

    //Kopplar objekt-textView till det i xml
    mPickedTimeText = (TextView) view.findViewById(R.id.text_picked_time);

    //Kopplar objekt-knapp till det i xml
    mPickTimeButton = (Button) view.findViewById(R.id.button_pick_time);

    //Skapar en setonclicklistener som säger vad som ska hända när knappen trycks
    mPickTimeButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            DialogFragment picker = new TimePickerFragment();;
            picker.show(getFragmentManager(), "timePicker");
        }
    });

    return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    setUserVisibleHint(true);
}

@Override
public void onTimePicked(Calendar time)
{
    // display the selected time in the TextView
    mPickedTimeText.setText(DateFormat.format("h:mm a", time));
}

}

所以我的问题是这个。在这段代码中:     picker.show(getFragmentManager(),&#34; timePicker&#34;);

我收到错误: DialogFragment类型中的方法show(FragmentManager,String)不适用于参数(FragmentManager,String&#34;)

现在我知道这可以通过扩展FragmentActivity来解决,但我不想要这个,因为我的ViewPagerAdapter.java将无法返回fragmenttab3。这使我处于一个有点Catch-22的位置。

如何在不扩展FragmentActivity的情况下解决此问题?

感谢我能得到的所有帮助。在圈子里跑了几个小时。

1 个答案:

答案 0 :(得分:1)

今天我花了很多时间寻找答案。我不确定正确的措辞/术语如何描述发生了什么,但放置getActivity()。getFragmentManager()将解决您的问题。