0

我正在尝试做一个我们需要使用片段的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> 

日志猫:

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
4

1 回答 1

1

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

<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>
于 2012-10-03T10:28:33.233 回答