使用片段管理器时,没有为add(int,Class name)找到合适的方法?

时间:2015-12-30 14:18:18

标签: android android-fragments

这是我正在使用的类。我正在尝试将片段中的数据转换为活动。

public class DetailsActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            finish();
            return;
        }

        if (savedInstanceState == null)
        {
            DetailsFragment details = new DetailsFragment();

            details.setArguments(getIntent().getExtras());

            getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();

        }
    }
}
  

我在getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();收到错误   这是我的DetailsFragment类的代码,包含所有导入,我希望这有助于解决问题。

package org.bordetuts.com.goldmine.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * Created by Anup Borde on 30-12-2015.
 */
public class DetailsFragment extends Fragment
{
    public static DetailsFragment newInstance(int index){
        DetailsFragment f = new DetailsFragment();
        Bundle args = new Bundle();
        args.putInt("index", index);

        f.setArguments(args);

        return f;
    }

    public int getShownIndex(){

        return getArguments().getInt("index",0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ScrollView scrollView = new ScrollView(getActivity());
        TextView text = new TextView(getActivity());

        int padding = (int)

                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                        4,getActivity().getResources().getDisplayMetrics());

        text.setPadding(padding,padding,padding,padding);

        scrollView.addView(text);
        text.setText("You selected "+ReportingTabs.tabNames[getShownIndex()]);

        return scrollView;
    }
}
  

感谢任何帮助。谢谢你

1 个答案:

答案 0 :(得分:1)

更改public class DetailsActivity extends Activity
public class DetailsActivity extends AppCompatActivity

getFragmentManager()更改为getSupportFragmentManager()