带有 Fragment 和 ViewPager 的 NullPointerException
Posted
技术标签:
【中文标题】带有 Fragment 和 ViewPager 的 NullPointerException【英文标题】:NullPointerException with Fragment and ViewPager 【发布时间】:2014-08-31 13:11:45 【问题描述】:我的代码有一些问题。我在下面的代码中有一个 NullPointerException。 我正在尝试在 3 个不同的片段之间创建滑动。
我不明白我的错误在哪里,所以请帮助我:p
DummyFragment.java:
package com.example.testslide;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DummyFragment extends Fragment
public static DummyFragment newInstance(String chaine)
DummyFragment fragment = new DummyFragment();
Bundle args = new Bundle();
args.putString("KEY_STRING", chaine);
fragment.setArguments(args);
return fragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.milieu, container);
TextView textView = (TextView) v.findViewById(R.id.textView1);
Bundle args = getArguments();
textView.setText(args.getString("KEY_STRING"));
return v;
ViewPagerAdapter
package com.example.testslide;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdapter extends FragmentPagerAdapter
public ViewPagerAdapter(FragmentManager fm)
super(fm);
@Override
public Fragment getItem(int pos)
switch(pos)
case 1: return DummyFragment.newInstance("Je suis le premier écran !");
case 2: return DummyFragment.newInstance("Je suis le second écran !");
case 3: return DummyFragment.newInstance("Je suis le troisième écran !");
return null;
@Override
public int getCount()
return 3;
ViewPagerActivity:
package com.example.testslide;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class ViewPagerActivity extends FragmentActivity
private ViewPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager);
// Set up the adapter.
mSectionsPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
viewpager.xml:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:id="@+id/viewPager">
</android.support.v4.view.ViewPager>
环境.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_margin="20dp"
android:text="Page du milieu" />
</LinearLayout>
LogCat:
07-10 08:25:26.211: E/AndroidRuntime(607): FATAL EXCEPTION: main
07-10 08:25:26.211: E/AndroidRuntime(607): java.lang.NullPointerException
07-10 08:25:26.211: E/AndroidRuntime(607): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.view.View.measure(View.java:10828)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.view.View.measure(View.java:10828)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.widget.LinearLayout.measureVertical(LinearLayout.java:613)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.view.View.measure(View.java:10828)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
07-10 08:25:26.211: E/AndroidRuntime(607): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1889)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.view.View.measure(View.java:10828)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.view.ViewRoot.performTraversals(ViewRoot.java:938)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.view.ViewRoot.handleMessage(ViewRoot.java:2040)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.os.Looper.loop(Looper.java:132)
07-10 08:25:26.211: E/AndroidRuntime(607): at android.app.ActivityThread.main(ActivityThread.java:4123)
07-10 08:25:26.211: E/AndroidRuntime(607): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 08:25:26.211: E/AndroidRuntime(607): at java.lang.reflect.Method.invoke(Method.java:491)
07-10 08:25:26.211: E/AndroidRuntime(607): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-10 08:25:26.211: E/AndroidRuntime(607): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-10 08:25:26.211: E/AndroidRuntime(607): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
【参考方案1】:我发现您的代码存在两个主要问题。
View v = inflater.inflate(R.layout.milieu, container);
容器应该只用于布局参数,而不是添加膨胀视图。此外,您使用的 inflate
版本会返回容器本身。所以你在错误的地方寻找你的观点。改变它
View v = inflater.inflate(R.layout.milieu, container, false);
position
是基于 0 的索引,因此如果您在 getCount
中返回 3,则 getItem
将与 position=0,1,2
一起调用。这意味着您正在为索引 0 返回 null
【讨论】:
以上是关于带有 Fragment 和 ViewPager 的 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
FragmentPagerAdapter 带有 ViewPager 和两个 Fragment。从第二个转到第一个并更新第一个的文本
如何从 MainActivity 中的 Fragment 访问适配器?我正在使用带有 viewpager 的标签,所以没有交易等方法
从 ViewPager Activity 访问 Fragment 的方法