2

这就是我正在做的事情:

在 ListActivity(不是 ListFragment)中:

Intent i = new Intent(getApplicationContext(), MyFragmentActivity.class);
i.putExtra("name", items.get(arg2));
startActivity(i);

然后在 MyFragmentActivity (使用支持库):

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_layout);

    this.initialiseTabHost(savedInstanceState);
    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));

        RateFrag rate = new RateFrag();
        rate.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, rate).commit();

    }

    this.intialiseViewPager();
}

然后在速率片段中:

 public class RateFrag extends Fragment {

  public static RateFrag newInstance(CharSequence name) {
    RateFrag f = new RateFrag();
    Bundle args = new Bundle();
    args.putCharSequence("name", name);
    f.setArguments(args);
    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Item = getArguments().getString("name");

}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    title = (TextView) getActivity().findViewById(R.id.tvRateItem);
    ratingsBar = (RatingBar) getActivity().findViewById(R.id.theRatingBar);

    title.setText(Item);

}

 // stuff!

 }

因此,我实际上是在尝试将原始 ListActivity 中的“额外”传递到上面最后一行代码的 Fragment 中的 textview 中。

这是我在单击 ListItem 时在 LogCat 中得到的:

07-09 19:12:39.998: E/AndroidRuntime(1788): FATAL EXCEPTION: main
07-09 19:12:39.998: E/AndroidRuntime(1788): java.lang.NullPointerException
07-09 19:12:39.998: E/AndroidRuntime(1788):     at com.---.---.RateFrag.onCreate(RateFrag.java:61)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:834)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:768)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:746)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:128)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.support.v4.view.ViewPager.populate(ViewPager.java:802)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:15172)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:15172)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:15172)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:15172)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:15172)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:15172)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:15172)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.Choreographer.doFrame(Choreographer.java:525)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.os.Handler.handleCallback(Handler.java:615)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.os.Looper.loop(Looper.java:137)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at java.lang.reflect.Method.invoke(Method.java:511)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-09 19:12:39.998: E/AndroidRuntime(1788):     at dalvik.system.NativeStart.main(Native Method)

将我的应用程序转换为使用 Fragments 让我抓狂!不可否认,最终产品的外观要好得多,其复杂性令人非常沮丧。有人可以指出我正确的方向,看看我在哪里失败了吗?

编辑:这是我的标签布局:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

            <TabWidget
                android:id="@android:id/tabs"
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0"/>

            <FrameLayout
                android:id="@+android:id/content"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
        </LinearLayout>
    </TabHost>
</LinearLayout>
4

1 回答 1

1

问题是您正试图在Fragment此处实例化两个...一个正在从 膨胀fragment_layout.xml,另一个正在以编程方式实例化并使用FragmentTransaction.

选择这两种方法中的一种来实例化您的Fragment......无论哪种选择最适合这种情况(我无法从您提供的最少代码中看出)。

于 2012-07-10T03:44:23.340 回答