![](/img/trans.png)
[英]Cyclic inheritance with Android development - fragments, activities and interfaces
[英]Error with fragments in Android development
我正处于开发应用程序的早期阶段,该应用程序将具有一个活动,其中包含通过滑动导航的4个片段。 目前,我要做的就是获取活动以加载/显示第一个Splash片段。 我将片段添加到main.xml中的活动中,如下所示。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.cdw.ddb.Splash"
android:id="@+id/splashfrag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
MainActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.splashfrag) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
Splash fragment = new Splash();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
fragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager()
.beginTransaction()
.add(R.id.splashfrag, fragment)
.commit();
}
}
我收到一个错误:
无法解析方法“ add(int,com.cdw.ddb.Splash)”
在这条线上
.add(R.id.splashfrag,片段)
从在线搜索看来,与此相关的最常见问题不是为活动声明“扩展FragmentActivity”或使用了错误的import语句。 我不相信这些都是我的问题。 对此问题的任何指导,我将不胜感激。 谢谢。
由于您正在使用支持库中的片段类,因此请确保您的Splash类正在扩展支持库的片段类(android.support.v4.app.Fragment)。 可能是您导入了android.app.Fragment。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.