0

This is the class that I am using .I am trying to get data from a fragment into an activity.

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

        }
    }
}

And I get an error at getFragmentManager().beginTransaction().add(android.R.id.content, details).commit(); And this is the code for my DetailsFragment class with all the imports I hope this will help solve the problem.

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;
    }
}

Any help is appreciated.Thank you

  • 1
    `DetailsFragment ` class probably extending Fragment from support lib. so try to use `getSupportFragmentManager()` instead of `getFragmentManager ` – ρяσѕρєя K Dec 30 '15 at 14:20
  • Thank you @Rohit_Ramkumar can you help! –  Dec 30 '15 at 14:20
  • @ρяσѕρєяK ok so what should I change coz if I change it to android.app.Fragment I still get an error in convertible types and If i use android.support.v4.app.Fragment; I get the above error –  Dec 30 '15 at 14:23
  • 1
    So if you are using support library then change getFragmentManager to getSupportFragmentManager – android_eng Dec 30 '15 at 14:25
  • You have to implement an interface for passing the data from `Fragment` to `Activity` See [this](http://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – Faraz Dec 30 '15 at 14:25
  • @Rohit_Ramkumar I am not able to use getSupportFragmentManager in this class –  Dec 30 '15 at 14:29
  • can you add your entire DetailsActivity class with all the imports – android_eng Dec 30 '15 at 14:29
  • @Rohit_Ramkumar please check the updated question –  Dec 30 '15 at 14:29

1 Answers1

2

Change public class DetailsActivity extends Activity
to public class DetailsActivity extends AppCompatActivity.

Change getFragmentManager() to getSupportFragmentManager()

tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • Thank you but It still gives me the same error.no suitable method found –  Dec 30 '15 at 14:34
  • you are using `import android.support.v4.app.Fragment;` in your fragment, so you WILL need to use `public class DetailsActivity extends FragmentActivity` (or AppCompatActivity) as @tinysunlight states. (that might not be 'all' you need to do, but it is among the things you need to do) – mawalker Dec 30 '15 at 14:39
  • https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html AppCompatActivity adds actionbar to api 7+ devices, but this extends FragmentActivity (is linked from that link) which is `Base class for activities that want to use the support-based Fragment and Loader APIs.` That linked 'Fragment' goes to android.support.v4.app.Fragment, which is what you are using. – mawalker Dec 30 '15 at 14:42
  • @anup I think FragmentActivity is enough. – tiny sunlight Dec 30 '15 at 14:46
  • It‘s easy to find answer by searching with your errorlog. – tiny sunlight Dec 30 '15 at 15:22