Android Gradle:错误找不到合适的替换方法

时间:2014-01-23 11:33:10

标签: android fragment navigation-drawer android-gradle

你好我一直在使用导航抽屉,当然这使用片段,一切都没问题,直到我遇到这个错误:

//(Radio is the name of the class i want to replace with)
Gradle: error: no suitable method found for replace(int,Radio)
method FragmentTransaction.replace(int,Fragment,String) is not applicable
(actual and formal argument lists differ in length)
method FragmentTransaction.replace(int,Fragment) is not applicable
(actual argument Radio cannot be converted to Fragment by method invocation conversion)

这是我的代码:

    @Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    if (position < 6){
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            .commit();

        PlaceholderFragment.navNumber = position + 1;
        PlaceholderFragment.isOnline = isOnline();
    } else  if (position == 6){
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, TvPlaceholderFragment.newInstance(position + 1))
                .commit();

        TvPlaceholderFragment.navNumber = position + 1;
        TvPlaceholderFragment.isOnline = isOnline();

    } else if (position == 7) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        //////////// THE ERROR IS HERE /////////////
        fragmentManager.beginTransaction()
                .replace(R.id.container, Radio.newInstance(position + 1))
                .commit();

        Radio.navNumber = position + 1;
        Radio.isOnline = isOnline();

    }


}

它与位置7以下的其他方法相同,所以我认为必须在Radio.class中,所以继承代码:

@TargetApi(11)
public class Radio extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";
    public static int navNumber = 0;
    public static boolean isOnline = false;
public WebView webView;
/**
 * Returns a new instance of this fragment for the given section
 * number.
 */
public static Radio newInstance(int sectionNumber) {
    Radio Fragment = new Radio();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    Fragment.setArguments(args);

    return Fragment;
}

public Radio() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.new_radio_main, container, false);

    webView = (WebView) rootView.findViewById(R.id.webView2);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(getString(R.string.myurl));
    return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((MainActivity) activity).onSectionAttached(
            getArguments().getInt(ARG_SECTION_NUMBER));
}

我不是新编程片段的新手。谁能帮我?非常感谢

1 个答案:

答案 0 :(得分:1)

您是否正在将Android框架类android.app.Fragment与支持库类android.support.v4.app.Fragment混合使用?或者混合其他框架/支持库类?检查你的进口。