没有找到add()错误的合适方法

时间:2016-11-07 07:39:14

标签: java android android-fragments

这是我的代码:

package com.example.wjdrmstn.myapplication;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

        if(findViewById(R.id.fragment_container) != null) {
            if(savedInstanceState != null)
                return;

            HeadlinesFragment firstFragment = new HeadlinesFragment();
            firstFragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,firstFragment).commit();  //error occurred
        }
    }
}

此代码来自android开发者网站:
https://developer.android.com/training/basics/fragments/fragment-ui.html

这是错误:

Error:(20, 59) error: no suitable method found for add(int,HeadlinesFragment)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable
(argument mismatch; HeadlinesFragment cannot be converted to Fragment)

编辑:HeadlinesFragment代码

package com.example.wjdrmstn.myapplication;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HeadlinesFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.headlines_view, container, false);
    }
}

1 个答案:

答案 0 :(得分:1)

更改您的导入

  import android.app.Fragment;

 import android.support.v4.app.Fragment;
相关问题