本文介绍了将getFragmentManager()与actionbarsherlock库一起使用时,java.lang.NoSuchMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我自己的项目使用:

<uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="13" />

要实现操作栏,我使用库。我将 sherlock 库作为现有项目导入到Eclipse中。对于 sherlock ,目标平台是 Android v3.2 API 13

To implement action bar, I uses ActionBarSherlock library. I imported the sherlock library into my Eclipse as an existing project. For sherlock, the target platform is Android v3.2 API 13 .

然后,我添加了 sherlock 作为我自己的项目的图书馆项目。我看到自己的项目下有图书馆项目。

Then, I added sherlock as a library project to my own project. I saw there is Library projects under my own project. Everything seem goes well.

我自己的项目主活动如下:

My own project main Activity looks like this:

package com.test;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MyActivity extends FragmentActivity {

    /**
     * hosts a Fragment, and the Fragment will inflate a layout to show
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fragMgr;
        FragmentTransaction fragTrans;

        fragMgr = getFragmentManager(); //error complains here


           FirstFragment list = new FirstFragment();

        fragTrans = fragMgr.beginTransaction();
        fragTrans.add(android.R.id.content, list).commit();

    }
}

但是,当我运行我的应用程序时,出现以下错误

But, when I run my application, I got the following error:

java.lang.NoSuchMethodError: com.test.MyActivity.getFragmentManager

为什么 getFragment 无法解析为一个方法??我已经将sherlock用作自己项目的项目库...

Why getFragment can not be resolved as a method?? I have used sherlock as a project library for my own project...

推荐答案

方法 getFragmentManager( )仅在 Activity 类中从3.0开始可用。由于您的目标sdk为13,并且 FragmentActivity Activity 扩展而来,因此在编译时没有问题。

The method getFragmentManager() is available only since 3.0, in the Activity class. Since your target sdk is 13, and FragmentActivity extends from Activity there are no problems at compilation time.

但是,当您在较低的sdk(例如7)上运行代码时, Activity 类中没有这种方法。因此解决方案是使用:

But when you run your code on a lower sdk (7 for example) there is no such method in that Activity class. So the solution is to use :

 getSupportFragmentManager();

可以在所有 Android 版本上使用。

which will work on all Android versions.

这篇关于将getFragmentManager()与actionbarsherlock库一起使用时,java.lang.NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 13:04