Android片段向后兼容

时间:2012-10-03 10:25:46

标签: android android-fragments

我正在尝试使用我们需要使用片段的android应用程序。我在执行向后兼容性时遵循规则。即扩展FragmentActivity而不是Activity,我也使用getSupportFragmentManager()。使用4.1但不使用2.2的相同代码。

我使用

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

String newTime = String.valueOf(System.currentTimeMillis());
DetailFragment fragment = (DetailFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
      fragment.setText(newTime);
    } else {
      Intent intent = new Intent(getActivity().getApplicationContext(),     DetailActivity.class);
      intent.putExtra("value", newTime);
      startActivity(intent);

任何人都可以帮忙吗?我正在接近这支部队。无法启动活动组件信息。 它在setContentView()主要活动中显示错误,其中包含分段。

<?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:id="@+id/listFragment"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"
        class="com.example.fragmentsample.MyListFragment" ></fragment>

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        class="com.example.fragmentsample.DetailFragment" >
        <!-- Preview: layout=@layout/details -->
    </fragment>

</LinearLayout> 

logcat的:

10-03 16:03:54.586: E/AndroidRuntime(649): FATAL EXCEPTION: main
10-03 16:03:54.586: E/AndroidRuntime(649): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentsample/com.example.fragmentsample.RssfeedActivity}: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.os.Looper.loop(Looper.java:123)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 16:03:54.586: E/AndroidRuntime(649):  at java.lang.reflect.Method.invokeNative(Native Method)
10-03 16:03:54.586: E/AndroidRuntime(649):  at java.lang.reflect.Method.invoke(Method.java:507)
10-03 16:03:54.586: E/AndroidRuntime(649):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-03 16:03:54.586: E/AndroidRuntime(649):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-03 16:03:54.586: E/AndroidRuntime(649):  at dalvik.system.NativeStart.main(Native Method)
10-03 16:03:54.586: E/AndroidRuntime(649): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3692)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1400)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1326)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:47)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:625)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-03 16:03:54.586: E/AndroidRuntime(649):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.app.Activity.setContentView(Activity.java:1657)
10-03 16:03:54.586: E/AndroidRuntime(649):  at com.example.fragmentsample.RssfeedActivity.onCreate(RssfeedActivity.java:10)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-03 16:03:54.586: E/AndroidRuntime(649):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-03 16:03:54.586: E/AndroidRuntime(649):  ... 11 more

1 个答案:

答案 0 :(得分:1)

在XML文件中,您需要提供Fragment的完整路径:

<android.support.v4.app.Fragment
android:id="@+id/detailFragment"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        class="com.example.fragmentsample.DetailFragment" >
        <!-- Preview: layout=@layout/details -->
    </android.support.v4.app.Fragment>