MainActivity.Java
这是我的代码导致启动应用程序时出现崩溃..只有在我评论OnCreate()
OnCreate方法
final TabLayout tb = (TabLayout) findViewById(R.id.tabs);
final ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new FragmentManagerAdapter(getSupportFragmentManager(),MainActivity.this));
tb.setupWithViewPager(pager);
记录我的项目
12-12 13:15:42.800 30536-30536/pk.com.messenger.rabta.tablayout E/AndroidRuntime: FATAL EXCEPTION: main
12-12 13:15:42.800 30536-30536/pk.com.messenger.rabta.tablayout E/AndroidRuntime: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
12-12 13:15:42.800 30536-30536/pk.com.messenger.rabta.tablayout E/AndroidRuntime: at pk.com.messenger.rabta.tablayout.FragmentManager.onCreateView(FragmentManager.java:38)
12-12 13:15:42.800 30536-30536/pk.com.messenger.rabta.tablayout E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
12-12 13:15:42.800 30536-30536/pk.com.messenger.rabta.tablayout E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
12-12 13:15:42.800 30536-30536/pk.com.messenger.rabta.tablayout E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
12-12 13:15:42.800 30536-30536/pk.com.messenger.rabta.tablayout E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
12-12 13:15:42.800 30536-30536/pk.com.messenger.rabta.tablayout E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
FragmentLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView1"
android:layout_gravity="center" />
</LinearLayout>
谢谢!
答案 0 :(得分:2)
在你的片段中这样做,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.yourFragment, container, false);
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText("Success");
return view;
}
以下评论以获取更多信息。