使用android.support.v4.app.FragmentActivity.getActionBar时“没有这样的方法错误”

时间:2014-01-04 06:25:42

标签: android fragment android-actionbar-compat

我正在尝试姜饼中的导航抽屉一切正常。当我点击导航菜单时,我得到了这个例外。我添加了所有库,主题,抽屉列表。实际上我需要片段中的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)

3 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:6)

我试过这段代码它工作正常

((ActionBarActivity) getActivity()).getSupportActionBar().setTitle(rivers[position]);

答案 2 :(得分:1)

您应该添加actionbarsherlock library Actionbar Sherlock for android

然后extends您的片段SherlockFragment 然后你可以采取

getActivity().getSupportActionbar().setTitle("");
希望它会对你有所帮助。