使用android sherlock编译片段示例时出错

使用android sherlock编译片段示例时出错,android,Android,我正在使用Android Sherlock,我正在尝试在Android中实现片段的经典示例: 但是在文件TitlesFragment.java中,避免运行我的应用程序时出错。我不知道为什么 package com.android.fragmenttest; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import andr

我正在使用Android Sherlock,我正在尝试在Android中实现片段的经典示例:

但是在文件TitlesFragment.java中,避免运行我的应用程序时出错。我不知道为什么

package com.android.fragmenttest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListFragment;

public class TitlesFragment extends SherlockListFragment {
    boolean mDualPane;
    int mCurCheckPosition = 0;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Populate list with our static array of titles.
        setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, Shakespeare.TITLES));

        // Check to see if we have a frame in which to embed the details
        // fragment directly in the containing UI.
        View detailsFrame = getActivity().findViewById(R.id.details);
        mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

        if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }

        if (mDualPane) {
            // In dual-pane mode, the list view highlights the selected item.
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            // Make sure our UI is in the correct state.
            showDetails(mCurCheckPosition);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putInt("curChoice", mCurCheckPosition);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
            showDetails(position);
    }

    // Helper function to show the details of a selected item, either by displaying a fragment in-place in the current UI, or starting a
    // whole new activity in which it is displayed.
    void showDetails(int index) {
        mCurCheckPosition = index;

        if (mDualPane) {
            // We can display everything in-place with fragments, so update the list to highlight the selected item and show the data.
            getListView().setItemChecked(index, true);

            // Check what fragment is currently shown, replace if needed.
            DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details);
            if (details == null || details.getShownIndex() != index) {
                // Make new fragment to show this selection.
                details = DetailsFragment.newInstance(index);

                // Execute a transaction, replacing any existing fragment with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.details, details);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }
        } else {
            // Otherwise we need to launch a new activity to display the dialog fragment with selected text.
            Intent intent = new Intent();
            intent.setClass(getActivity(), DetailsActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }
    }
}
以及:

以及:

因此,R.id.details就是问题所在。类DetailsFragment.java定义如下:

package com.android.fragmenttest;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

public class DetailsFragment extends SherlockFragment {
    // Create a new instance of DetailsFragment, initialized to show the text at 'index'.
    public static DetailsFragment newInstance(int index) {
        DetailsFragment f = new DetailsFragment();

        // Supply index input as an argument.
        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) {
        if (container == null) {
            // We have different layouts, and in one of them this fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }
        ScrollView scroller = 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);
        scroller.addView(text);
        text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
        return scroller;
    }
}

有人知道为什么Eclipse不让这个片段示例编译吗?

应该是
getSherlockActivity()
getSupportFragmentManager()使用ActionbarSherlock时。

它应该是
getSherlockActivity()
getSupportFragmentManager()使用ActionbarSherlock时。

如果不包含错误日志,您希望有人如何帮助您?来吧对不起,奥利。这是我的第一个话题。谢谢你的建议。当你没有包含错误日志时,你希望有人如何帮助你?来吧对不起,奥利。这是我的第一个话题。谢谢你的建议。哦,就是这样!谢谢你的帮助。@graffiti75选择这个答案作为正确答案是你应该采取的方法,而不是评论“就是这样!”哦,就是这样!谢谢你的帮助。@graffiti75选择这个答案作为正确答案是你应该采取的方法,而不是评论“就是这样!”
DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details);
ft.replace(R.id.details, details);
package com.android.fragmenttest;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

public class DetailsFragment extends SherlockFragment {
    // Create a new instance of DetailsFragment, initialized to show the text at 'index'.
    public static DetailsFragment newInstance(int index) {
        DetailsFragment f = new DetailsFragment();

        // Supply index input as an argument.
        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) {
        if (container == null) {
            // We have different layouts, and in one of them this fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }
        ScrollView scroller = 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);
        scroller.addView(text);
        text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
        return scroller;
    }
}