当前位置: 代码迷 >> Android >> Fragment如何使用
  详细解决方案

Fragment如何使用

热度:43   发布时间:2016-04-28 06:22:48.0
Fragment怎么使用?
Android SDK ver. 22.3
Android SDK Platform-tools ver.19

---------------------------------------------------------------------------------------------


fragment_test_one.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="horizontal" >
    
    <fragment android:name="HeadlinesFragment"
        android:id="@+id/headlines_fragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>
    <fragment android:name="ArticleFragment"
        android:id="@+id/article_framgment"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

</LinearLayout>


MainActivity:
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.fragment_test_one);



}}


一运行就闪退。。。什么问题??
------解决方案--------------------
在谷歌官网上Fragmen的例子xml文档是这样写的:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>
你在你的xml文件中也添加上class试试。
------解决方案--------------------
android:name要写全名。
------解决方案--------------------
不知道他有没有建好Fragment,要不然怎么会这样写
------解决方案--------------------
引用:
Quote: 引用:

不知道他有没有建好Fragment,要不然怎么会这样写
我写全了怎么还是不行?

Fragment可以看做是子Activity,你确定你建了Fragment类,继承了Fragment,配置了xml文件?
建议你先下点源码或者看看文档再说
  相关解决方案