2011-11-08 69 views
3

我试图让我的脑袋周围的碎片为了让我的应用准备好ICS。基本片段布局的困难

我有以下文件只是得到最基本的片段应用程序,你可以有。启动时应该有这个:一个片段布局,文本视图为“片段1”,另一个片段布局为“片段2”。

我的包的名字是com.mwerner.fragments

我的文件有:

  • FragmentsActivity.java
  • ExamplesFragment.java
  • ExamplesFragment2.java
  • examples_fragment.xml
  • examples_fragment2.xml
  • main.xml中

为FragmentsActivity.java的代码是:

package com.mwerner.fragments; 

import android.app.Activity; 
import android.os.Bundle; 

public class FragmentsActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

ExamplesFragment.java

package com.mwerner.fragments; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class ExamplesFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.examples_fragment, container, false); 
    } 
} 

ExamplesFragment2.java

package com.mwerner.fragments; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class ExamplesFragment2 extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.examples_fragment2, container, false); 
    } 
} 

的examples_fragment.xml文件只需要一个线性布局extview在它... 下面是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" > 
<fragment 
     class="com.mwerner.fragments$ExamplesFragment" 
     android:id="@+id/list" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     /> 

<fragment 
     class="com.mwerner.fragments$ExamplesFragment2" 
     android:id="@+id/viewer" 
     android:layout_weight="2" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" /> 


</LinearLayout> 

上启动该应用程序崩溃,并显示错误

11-07 18:12:12.519: E/AndroidRuntime(696): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mwerner.fragments/com.mwerner.fragments.FragmentsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 

你能告诉我这里有什么问题?我几乎从谷歌开发者页面复制/粘贴代码片段。

+0

你解决了这个问题吗?我注意到你正在输入错误的'Fragment'。而不是'android.support.v4.app.Fragment',你应该使用'android.app.Fragment'。 – adneal

+0

正确,你应该导入android.app.Fragment; – AshesToAshes

回答

5

您在布局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" > 
<fragment 
     class="com.mwerner.fragments.ExamplesFragment" 
     android:id="@+id/list" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     /> 

<fragment 
     class="com.mwerner.fragments.ExamplesFragment2" 
     android:id="@+id/viewer" 
     android:layout_weight="2" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" /> 


</LinearLayout> 
+0

我仍然得到相同的错误: 11-07 18:39:35.524:E/AndroidRuntime(790):java.lang.RuntimeException:无法启动活动ComponentInfo {com.mwerner.fragments/com.mwerner.fragments。 FragmentsActivity}:android.view.InflateException:二进制XML文件行#6:错误膨胀类片段 – Killerpixler

+0

你能帮助我吗?我试过你的xml文件,但仍然得到上面提到的错误(启动时崩溃)。任何帮助将非常感谢 – Killerpixler

+0

有同样的问题。我的问题也是类属性 - 我在我的布局中定义的命名空间不正确(与我的Fragment类中的命名空间不匹配)。 – AshesToAshes

0

尝试延长FragmentActivity

public class FragmentsActivity extends FragmentActivity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
     } 
    } 
+0

不知道哪个版本的评论是反对的,但是当我这样做时,我仍然得到膨胀异常。你应该保持Activity来扩展Dialog。 – AshesToAshes

0

当我使用我发生同样的错误片段写我的第一个程序。而不是扩展“活动”在您的启动器活动中扩展“FragmentActivity”。

+0

不知道哪个版本的评论是反对的,但是当我这样做时,我仍然得到膨胀异常。你应该保持Activity来扩展Dialog。 – AshesToAshes