2

I've seen other posts, and they all suggest that the host activity java file is not extending android.support.v4.app.Fragment. However, I don't know how to make the hosting java file extend android.support.v4.app.Fragment. I've tried adding the dependency to the gradle file but that doesn't work either.

Here's the hosting activity snippet:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import course.examples.fragmentstaticlayout.TitlesFragment.ListSelectionListener;

public class QuoteViewerActivity extends Activity implements ListSelectionListener{

    public static String[] mTitleArray;
    public static String[] mQuoteArray;
    private QuotesFragment mDetailsFragment;

    private static final String TAG = "QuoteViewerActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTitleArray = getResources().getStringArray(R.array.Titles);
        mQuoteArray = getResources().getStringArray(R.array.Quotes);

        setContentView(R.layout.main);

        mDetailsFragment = (QuotesFragment) getFragmentManager().findFragmentById(R.id.details);
    }

The line in question is the last line of the snippet. And here is my QuotesFragment.

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class QuotesFragment extends Fragment {

    private TextView mQuoteView = null;
    private int mCurrIdx = -1;
    private int mQuoteArrayLen;

    private static final String TAG = "QuotesFragment";

    public int getShownIndex() {
        return mCurrIdx;
    }

    public void showQuoteAtIndex(int newIndex) {
        if (newIndex < 0 || newIndex >= mQuoteArrayLen)
            return;
        mCurrIdx = newIndex;
        mQuoteView.setText(QuoteViewerActivity.mQuoteArray[mCurrIdx]);
    }

    @Override
    public void onAttach(Context context){
        Log.i(TAG, getClass().getSimpleName() + ":entered onAttach()");

        super.onAttach(context);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, getClass().getSimpleName() + ":entered onCreate()");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.quote_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mQuoteView = (TextView) getActivity().findViewById(R.id.quoteView);
        mQuoteArrayLen = QuoteViewerActivity.mQuoteArray.length;

    }

Assuming that I'm right about the host activity not extending the support.v4, how would I do this?

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
jason adams
  • 545
  • 2
  • 15
  • 30
  • 1
    obviously, you are mixing new API(13) with supportAPI .... fx: `getFragmentManager()` it doesn't exist in older API ... also if you wana support older API than 13 you should remeber that it knows nothing about Fragments ... also basics inheritance: `android.app.Fragment` is not a base class of `android.support.v4.app.Fragment` – Selvin Dec 11 '15 at 23:50

2 Answers2

4

This is because you are trying to cast Fragment to SupportFragment.

Your QuotesFragment is extending SupportFragment, and while casting you are trying to use FragmentManager and fragment which returns Fragment, not supportFragment.

Try using geSupportFragmentManager instead getFragmentManager.

Also you should extend AppCompatActivity or FragmentActivity instead of Activity for your QuoteViewerActivity

Here is similar question

Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • is there a preference difference between AppCompatActivity and FragmentActivity? Meaning is using one better than the other one? – jason adams Dec 12 '15 at 00:02
  • 1
    Usecases are different, AppcompatActivity is latest thing. @jasonadams see this >> http://stackoverflow.com/a/31297546/519718 – AAnkit Dec 12 '15 at 00:47
1

Try changing your getFragmentManager() to getSupportFragmentManager() and extending FragmentActivity instead of Activity in your QuoteViewerActivity.

lodlock
  • 3,638
  • 1
  • 19
  • 15