我偶然发现了需要帮助的问题。
我在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(),“ timePicker”);

我收到错误消息:DialogFragment类型的show(FragmentManager,String)方法不适用于参数(FragmentManager,String“)

现在我知道可以通过扩展FragmentActivity来解决此问题,但是我不希望这样做,因为ViewPagerAdapter.java将无法返回fragmenttab3。这让我处于Catch-22的位置。

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

感谢您能获得的所有帮助。在圈子里跑了两个小时。

最佳答案

我今天花了很多时间来寻找答案。我不确定如何描述发生的事情的正确措辞/行话,但是放置getActivity()。getFragmentManager()会解决您的问题。

09-30 00:08