【问题标题】:Android Fragments on lower API较低 API 上的 Android 片段
【发布时间】:2012-05-07 08:16:50
【问题描述】:

好吧,我开始了解 Android 片段,但这仍然让我感到困惑。我需要一点帮助。正如它所说,自 API 级别 11 起支持 android 片段,但您可以下载“support.v4”库包以获取较低级别的 API。好吧,首先我创建新项目来尝试 API 级别 15 上的片段。然后我对 API 级别 8 做了同样的事情,但它不起作用......我导入了外部 jar,它看到了所有需要的导入......是这里的问题吗?

这是我的主要课程:

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>

我的片段类:

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

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

还有我的片段布局 XML:

<?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 api android-fragments


【解决方案1】:

您仍在使用不存在的android.view.fragment 类。您必须在旧设备上切换到 android.support.v4.app.fragment

【讨论】:

  • 它实际上是 'android.support.v4.app.Fragment' 我正在使用它;/
  • 显然不是无处不在:ClassNotFoundException: android.view.fragment
  • 这是我的片段类导入... ;/ import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
  • 我尝试将 extends Activity 更改为 extends FragmentActivity 并从 support.v4 包中导入 FragmentActivity 类...还是一样...
【解决方案2】:

如果您因为 minSdkVersion 而使用较低的 API,您应该知道您可以使用最新的 sdk 并将 minSdkVersion 设置为较低的值。它们不必相同。

【讨论】:

  • 如果我这样做,我仍然无法在 Android 模拟器 2.2 上运行它,但我仍然可以在 4.0.3 上运行,我在 manifest ` `
【解决方案3】:

其实这个问题是来自 ADT 工具的。

  1. 在 Java 构建路径中重新排序“排序和导出” src > gen > Private > 其他...

  2. 添加支持库 >> android-support-v4.jar

【讨论】:

    【解决方案4】:

    好的,我遇到了同样的问题,终于让它工作了。首先确保您拥有最新的 ADT 工具。

    1)进入每个使用片段的活动,并让它们从FragmentActivity而不是Activity扩展

    2)如果您还没有将支持库导入到您的构建中,则右键单击您的项目->Android 工具->添加支持库

    3) 确保您的 xml 中的标签是 &lt;fragment /&gt; 而不是 &lt;android.support.v4.app.fragment /&gt;

    希望对您有所帮助,我花了一段时间才开始工作。

    【讨论】:

      猜你喜欢
      • 2013-09-13
      • 2015-11-13
      • 2015-12-12
      • 2015-02-05
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多