我试着在姜饼导航抽屉一切正常。当我点击导航菜单我得到这个例外。我添加了一切图书馆,主题,抽屉列表。实际上,我需要片段中的getsupportActionBar(),所以如何获取它请帮助我。
代码:

public class RiverFragment extends Fragment{


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Retrieving the currently selected item number
        int position = getArguments().getInt("position");

        // List of rivers
        String[] rivers = getResources().getStringArray(R.array.rivers);

        // Creating view correspoding to the fragment
        View v = inflater.inflate(R.layout.fragment_layout, container, false);

        // Getting reference to the TextView of the Fragment
        TextView tv = (TextView) v.findViewById(R.id.tv_content);

        // Setting currently selected river name in the TextView
        tv.setText(rivers[position]);

        // Updating the action bar title
        getActivity().getActionBar().setTitle(rivers[position]);


        return v;
    }
}

例外:
01-04 11:47:21.601: E/AndroidRuntime(20537): FATAL EXCEPTION: main
01-04 11:47:21.601: E/AndroidRuntime(20537): java.lang.NoSuchMethodError: android.support.v4.app.FragmentActivity.getActionBar
01-04 11:47:21.601: E/AndroidRuntime(20537):    at in.wptrafficanalyzer.navigationdrawerdemo.RiverFragment.onCreateView(RiverFragment.java:36)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at android.os.Handler.handleCallback(Handler.java:587)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at android.os.Looper.loop(Looper.java:130)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at java.lang.reflect.Method.invokeNative(Native Method)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at java.lang.reflect.Method.invoke(Method.java:507)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-04 11:47:21.601: E/AndroidRuntime(20537):    at dalvik.system.NativeStart.main(Native Method)

最佳答案

您的活动必须扩展ActionBarActivity而不是FragmentActivity(注意ActionBarActivity扩展FragmentActivity-它只是通过getSupportActionBar()添加对操作栏的支持)。

关于android - 使用android.support.v4.app.FragmentActivity.getActionBar时出现“无此类方法错误”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20917759/

10-10 04:26