底层API上的Android片段
嗯,我开始理解Android的片段,但这对我来说仍然很困惑。我需要一点帮助。正如它所说的,android片段从API级别11开始就受到支持,但您可以下载用于较低级别API的“support.v4”库包。首先,我创建了一个新项目来尝试API级别15上的片段。然后我对API级别8做了同样的操作,但它不起作用。。。我导入了外部jar,它看到了所有需要导入的内容。。。这里有什么问题吗 这是我的主要课程:底层API上的Android片段,android,api,android-fragments,Android,Api,Android Fragments,嗯,我开始理解Android的片段,但这对我来说仍然很困惑。我需要一点帮助。正如它所说的,android片段从API级别11开始就受到支持,但您可以下载用于较低级别API的“support.v4”库包。首先,我创建了一个新项目来尝试API级别15上的片段。然后我对API级别8做了同样的操作,但它不起作用。。。我导入了外部jar,它看到了所有需要导入的内容。。。这里有什么问题吗 这是我的主要课程: import android.support.v4.app.FragmentActivity; im
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class Fragment2Activity extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
我的主要布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<fragment
android:name="com.frag.Fragment2.frag"
android:id="@+id/frag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="THIS IS FRAGMENT"
android:background="@drawable/ic_launcher"
/>
</LinearLayout>
和我的片段布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<fragment
android:name="com.frag.Fragment2.frag"
android:id="@+id/frag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="THIS IS FRAGMENT"
android:background="@drawable/ic_launcher"
/>
</LinearLayout>
已修复您仍在使用不存在的
android.view.fragment
类。在较旧的设备上,您必须切换到android.support.v4.app.fragment。如果因为Minsdk版本而使用较低的API,您应该知道可以使用最新的sdk并将Minsdk版本设置为较低的值。它们必须是不同的。好的,所以我有相同的问题,最后让它工作了。首先,确保您拥有最新的ADT工具
1) 进入每个使用片段的活动,让它们从FragmentActivity
扩展而不是activity
2) 如果尚未将支持库导入到构建中,请右键单击项目->Android工具->添加支持库
3) 确保xml中的标记是
,而不是
希望这对您有所帮助,我花了一些时间让它工作。实际上,这个问题来自ADT工具
它实际上是“android.support.v4.app.Fragment”,我正在使用它/显然不是到处都是:
ClassNotFoundException:android.view.fragment
这是我的片段类导入…;/<代码>导入android.os.Bundle;导入android.support.v4.app.Fragment;导入android.view.LayoutInflater;导入android.view.view;导入android.view.ViewGroup代码>我尝试将扩展活动
更改为扩展碎片活动
并从support.v4 pack导入碎片活动类。。。还是一样…如果我这样做了,我仍然无法在Android emulator 2.2上运行它,但我仍然可以在4.0.3上运行它,我在清单上更改了它``你能工作吗?我也有同样的问题,你的解决方案是什么?