这就是我在做的事情:
在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();
}
然后在Rate Fragment中:
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中的“额外”传递到上面代码最后一行片段中的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)
将我的应用转换为使用片段让我疯了!不可否认,最终产品看起来更好看,其复杂性令人非常沮丧。有人能指出我正确的方向,看看我失败了吗?
编辑:这是我的标签布局:
<?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>
答案 0 :(得分:1)
问题是你试图在这里实例化两个Fragment
...一个是从fragment_layout.xml
膨胀,另一个是以编程方式实例化并放在屏幕上{{1} 1}}。
选择这两种这两种方法来实例化你的FragmentTransaction
......最适合这种情况的选项(我无法从你提供的最小代码中得知)。< / p>