无法投射android.app.fragment

时间:2015-12-11 23:46:52

标签: java android android-fragments android-support-library

我见过其他帖子,他们都建议主机活动java文件不扩展android.support.v4.app.Fragment。但是,我不知道如何使托管java文件扩展android.support.v4.app.Fragment。 我已经尝试将依赖项添加到gradle文件,但这也不起作用。

这是托管活动代码段:

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

有问题的行是代码段的最后一行。这是我的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;

    }

假设我对主机活动没有扩展support.v4是正确的,我该怎么做?

2 个答案:

答案 0 :(得分:4)

这是因为您正在尝试将Fragment投射到SupportFragment

您的QuotesFragment正在延长SupportFragment,在投射时,您尝试使用返回片段的FragmentManager and fragment,而不是supportFragment

尝试使用geSupportFragmentManager代替getFragmentManager

此外,您应该为QuoteViewerActivity扩展AppCompatActivity或FragmentActivity而不是Activity

Here is similar question

答案 1 :(得分:1)

尝试将getFragmentManager()更改为getSupportFragmentManager()并在FragmentActivity中扩展Activity而不是QuoteViewerActivity