Reputation: 565
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?
Upvotes: 2
Views: 3183
Reputation: 27549
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
Upvotes: 4
Reputation: 3788
Try changing your getFragmentManager()
to getSupportFragmentManager()
and extending FragmentActivity
instead of Activity
in your QuoteViewerActivity
.
Upvotes: 1