我想制作滑动标签,这里卡住了:
Main.java
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Window;
public class Main extends ActionBarActivity implements TabListener, android.support.v7.app.ActionBar.TabListener {
android.support.v7.app.ActionBar actionbar;
ViewPager viewpager;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_main);
viewpager = (ViewPager)findViewById(R.id.pager);
viewpager.setAdapter(new FragmentAdapter(getSupportFragmentManager()));
actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
android.support.v7.app.ActionBar.Tab tab_1 = actionbar.newTab();
android.support.v7.app.ActionBar.Tab tab_2 = actionbar.newTab();
tab_1.setText("Tab 1");
tab_2.setText("Tab 2");
tab_1.setTabListener(this);
tab_2.setTabListener(this);
actionbar.addTab(tab_1);
actionbar.addTab(tab_2);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
class FragmentAdapter extends FragmentPagerAdapter{
public FragmentAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment frag=null;
if(arg0 == 0)
{
frag = new TAB_1();
}
if(arg0 == 1)
{
frag = new TAB_2();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
activity_main.xml中
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_height = "match_parent"
android:layout_width="match_parent"
></android.support.v4.view.ViewPager>
我对我的代码进行了调查,发现问题出现在这一行
viewpager.setAdapter(new FragmentAdapter(getSupportFragmentManager()));
函数getSupportFragmentManager()
返回null。
我创建了FragmentAdapter类,它从FragmentPagerAdapter扩展。
我的堆栈跟踪:
06-09 14:15:16.536: E/AndroidRuntime(20977): FATAL EXCEPTION: main
06-09 14:15:16.536: E/AndroidRuntime(20977): Process: com.MX.KIK, PID: 20977
06-09 14:15:16.536: E/AndroidRuntime(20977): java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:416)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:411)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.support.v4.view.ViewPager.populate(ViewPager.java:987)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5801)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5801)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5801)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5801)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1835)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.widget.LinearLayout.measureVertical(LinearLayout.java:725)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.widget.LinearLayout.onMeasure(LinearLayout.java:616)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5801)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
06-09 14:15:16.536: E/AndroidRuntime(20977): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2881)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2237)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1295)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1537)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.Choreographer.doCallbacks(Choreographer.java:590)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.Choreographer.doFrame(Choreographer.java:560)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.os.Handler.handleCallback(Handler.java:739)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.os.Handler.dispatchMessage(Handler.java:95)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.os.Looper.loop(Looper.java:145)
06-09 14:15:16.536: E/AndroidRuntime(20977): at android.app.ActivityThread.main(ActivityThread.java:5832)
06-09 14:15:16.536: E/AndroidRuntime(20977): at java.lang.reflect.Method.invoke(Native Method)
06-09 14:15:16.536: E/AndroidRuntime(20977): at java.lang.reflect.Method.invoke(Method.java:372)
06-09 14:15:16.536: E/AndroidRuntime(20977): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
06-09 14:15:16.536: E/AndroidRuntime(20977): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
提前致谢!
答案 0 :(得分:0)
你getItem
必须返回Fragment
的实例,而不是null。将其更改为:
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment frag=null;
if (arg0 == 0) {
frag = new TAB_1();
} else if(arg0 == 1) {
frag = new TAB_2();
}
return frag;
}
答案 1 :(得分:0)
@Blackbelt是绝对正确的......因为你从getItem方法返回null ...你需要从它返回片段的引用。
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment frag=null;
if (arg0 == 0) {
frag = new TAB_1();
} else if(arg0 == 1) {
frag = new TAB_2();
}
return frag;
}
[http://developer.android.com/training/implementing-navigation/lateral.html]
点击此链接以获取有关您希望实现的内容的更多信息..