gpt4 book ai didi

android - ViewPager 适配器上的空异常 - Android

转载 作者:行者123 更新时间:2023-11-30 00:35:28 26 4
gpt4 key购买 nike

我正在尝试使用我的适配器在 Android 中实现 ViewPager 并收到此错误,我用谷歌搜索了一下,一切似乎都很好,但我的应用程序仍然崩溃,我们将不胜感激。

适配器:

public class MainPagerAdapter extends FragmentPagerAdapter {

public MainPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch (position){
case 0:
ChatFragment.create();
case 1:
EmptyFragment.create();
case 2:
StoryFragment.create();
}
return null;
}

@Override
public int getCount() {
return 3;
}

主要 Activity :

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

View back = (View) findViewById(R.id.am_background_view);
ViewPager pager = (ViewPager) findViewById(R.id.am_view_pager);
MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
}

主 Activity .xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<View
android:id="@+id/am_background_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/black"
/>




<android.support.v4.view.ViewPager
android:id="@+id/am_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

聊天 fragment

public class ChatFragment extends Fragment {

public static ChatFragment create() {
return new ChatFragment();
}


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_chat, container, false);
return view;
}

错误:

FATAL EXCEPTION: main
Process: pk.fastech.snappy.snappy, PID: 6148
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:380)
at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:375)
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:103)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1182)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:20158)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
at android.view.View.measure(View.java:20158)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2644)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1599)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1891)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1487)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7450)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

最佳答案

您的 getItem 方法不正确,总是返回 null

你应该把它改成:

@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return ChatFragment.create();
case 1:
return EmptyFragment.create();
case 2:
return StoryFragment.create();
}
return null;
}
Java 中的

return 将结果返回给调用方法 - 但您并未返回此结果 - 它不会“堆叠”。

关于android - ViewPager 适配器上的空异常 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43476520/

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