Android v4 getFragmentManager with Activity - 不兼容的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25321222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
v4 getFragmentManager with Activity - Incompatible types
提问by David
I have a simple activity which runs as expected.
我有一个按预期运行的简单活动。
import android.app.Activity;
import android.app.FragmentManager;
// import android.support.v4.app.FragmentManager;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// FragmentManager fm = getSupportFragmentManager(); // ActionBarActivity
FragmentManager fm = getFragmentManager(); // Activity
}
}
I then replaced
然后我更换了
import android.app.FragmentManager;
with
和
import android.support.v4.app.FragmentManager;
so I could support my older devices.. However, this reports an error:
所以我可以支持我的旧设备..但是,这报告了一个错误:
Incompatible types.
Required: android.support.v4.app.FragmentManager
Found: android.app.FragmentManager
What am I doing wrong here?
我在这里做错了什么?
The popular solution I found is to use getSupportFragmentManager() instead, but this only works for ActionBarActivites [edit - see answers] and FragmentActivities.
我发现的流行解决方案是改用 getSupportFragmentManager(),但这仅适用于 ActionBarActivites [编辑 - 查看答案] 和 FragmentActivities。
cannot convert from android.app.FragmentManager to android.support.v4.app.FragmentManager
无法从 android.app.FragmentManager 转换为 android.support.v4.app.FragmentManager
The other relevant solution points to using a FragmentActivity instead, but this appears to have the same legacy problems.
另一个相关的解决方案指向使用 FragmentActivity 代替,但这似乎具有相同的遗留问题。
The method getFragmentManager() is undefined for the type MyActivity
未为 MyActivity 类型定义 getFragmentManager() 方法
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// FragmentManager fm = getSupportFragmentManager(); // ActionBarActivity
FragmentManager fm = getFragmentManager();
}
}
I'm pretty sure the solution to this will be on SE already, but it isn't easy (for me) to find. The minimal example should help other people with understanding it, too.
我很确定这个问题的解决方案已经在 SE 上,但是(对我来说)不容易找到。最小的例子也应该帮助其他人理解它。
- I'm fairly new to Android.
- 我对Android相当陌生。
回答by Raghunandan
In your first case if you use getSupportFragmentManager()
you need to extend FragmentActivity
or extend ActionBarActivity
(extends FragmentActivity) as FragmentActivity
is the base class for support based fragments.
在您的第一种情况下,如果您使用getSupportFragmentManager()
,则需要扩展FragmentActivity
或扩展ActionBarActivity
(扩展 FragmentActivity)作为FragmentActivity
基于支持的片段的基类。
In your second case you need to use getSupportFragmentManager()
instead of getFragmentManager()
.
在您的第二种情况下,您需要使用getSupportFragmentManager()
而不是getFragmentManager()
.
Fragments were introduced in honeycomb. To support fragments below honeycomb you need to use fragments from the support library in which case you need to extend FragmentActivity
and use getSupportFragmentManager()
.
碎片被引入蜂窝中。要支持蜂窝下方的片段,您需要使用支持库中的片段,在这种情况下,您需要扩展FragmentActivity
和使用getSupportFragmentManager()
.
回答by Rod_Algonquin
use getSupportFragmentManager() instead, but this only works for ActionBarActivites.
Wrong, it should work in your FragmentActivity
, that FragmentActivity is in your support package, when you are supporting older device then all your import from activity
,fragment
, fragment managers
, etc. must have android.support.v4.
to specify that you are using the support package. without using so will result to incompatible compile time error.
错了,应该在你的工作FragmentActivity
,这是FragmentActivity在您的支持包,当你从支持旧的设备,那么所有的进口activity
,fragment
,fragment managers
,等必须android.support.v4.
指定要使用的支持包。不使用 so 会导致incompatible compile time error.
What am I doing wrong here?
You are combining support package with non support package which result to incompatible compile time error
, as I said above.
incompatible compile time error
正如我上面所说,您正在将支持包与非支持包相结合,从而导致。
回答by Goodlife
for Kotlin guyz u dont have to change to not v4
对于 Kotlin Guyz,您不必更改为非 v4
use below to get the activity as long as its the parent
使用下面来获取活动,只要它是父项
(activity as MainDrawerActivity)
or
或者
((YourActivityName)getActivity());
回答by Maryam Azhdari
For use in Java Dialog:
在 Java 对话框中使用:
- import android.app.FragmentManager;
- FragmentManager fragmentManager;
For Kotlin :
对于科特林:
- (activity as? YourActivity)?.fragmentManager