gpt4 book ai didi

java - (Viewpager 适配器)getSupportFragmentManager() 返回 NULL

转载 作者:太空宇宙 更新时间:2023-11-04 13:46:56 27 4
gpt4 key购买 nike

我想制作滑动标签,但我卡住了:

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 。我创建了从 FragmentPagerAdapter 扩展的 FragmentAdapter 类。

我的堆栈跟踪:

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)

提前致谢!

最佳答案

您的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;
}

关于java - (Viewpager 适配器)getSupportFragmentManager() 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30731832/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com