在android中使用getfragmentmanager()时“没有这样的方法错误”

时间:2012-12-07 05:48:19

标签: android android-fragments android-dialogfragment

在我的应用程序中,我使用Dialog片段作为启动画面。因此它将在应用启动时显示启动画面。当我在Android版本3下面的设备上运行应用程序时,它会显示“No such method error:getFragmentManager”之类的错误。我已经看到它可以通过使用getSupportFragmentManager()来解决。但是当我使用它时,Splash对话框是没有显示。请有人帮助我......谢谢你...

主要活动=>

        ft = getFragmentManager().beginTransaction(); //Here I am using getFragmentManager
    newFragment = SplashDialogFragment.newInstance();
    newFragment.show(MainActivity.ft, "dialog");

这是我的Splash Screen Fragment =>

public class SplashDialogFragment extends DialogFragment {

    private EditText mEditText;


    static SplashDialogFragment newInstance() {
        SplashDialogFragment f = new SplashDialogFragment();
        return f;
    }
    public void EditNameDialog() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme);        
    }

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

        View view = inflater.inflate(R.layout.splash_fragment, container);


        return view;
    }

    public void show(FragmentManager supportFragmentManager, String string) {
        // TODO Auto-generated method stub


    }


}

和erro log =>

 > 12-07 11:09:09.330: E/AndroidRuntime(628): FATAL EXCEPTION: main
    12-07 11:09:09.330: E/AndroidRuntime(628): java.lang.NoSuchMethodError: com.myapp.mlm.MainActivity.getFragmentManager
    12-07 11:09:09.330: E/AndroidRuntime(628):  at com.madhyamam.malayalam2.MainActivity.onCreate(MainActivity.java:103)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at android.os.Handler.dispatchMessage(Handler.java:99)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at android.os.Looper.loop(Looper.java:123)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at java.lang.reflect.Method.invokeNative(Native Method)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at java.lang.reflect.Method.invoke(Method.java:507)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    12-07 11:09:09.330: E/AndroidRuntime(628):  at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

我通过将MainActivity扩展到FragmentActivity解决了这个问题。以前它正在扩展Activity.After之后我使用了getSupportFragmentManager没有任何问题。

答案 1 :(得分:0)

android.support.v4.app.Fragment;因为片段在API级别11中可用。 请看这个链接: - http://developer.android.com/reference/android/support/v4/app/package-summary.html

和这个链接: -

java.lang.NoSuchMethodError when use getFragmentManager() with actionbarsherlock library

将支持jar文件放在libs中添加到build path文件夹中并导入类似于android.support.v4.app.Fragment,而不是之前的。          希望这会对你有所帮助。

答案 2 :(得分:0)

很明显它会在版本低于3时失败。请看一下 http://developer.android.com/tools/extras/support-library.html 您可以尝试使用兼容性jar。

这可能会对你有所帮助 http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/