java-具有Fragment和ViewPager的NullPointerException

java-具有Fragment和ViewPager的NullPointerException,第1张

概述我的代码有问题.我在下面的代码中有一个NullPointerException.我正在尝试在3个不同片段之间创建滑动.我不明白我的错误在哪里,所以请帮助我:pDummyFragment.java:packagecom.example.testslide;importandroid.os.Bundle;importandroid.support.v4.app.Fragment;importand

我的代码有问题.我在下面的代码中有一个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_wIDth="fill_parent"    androID:layout_height="fill_parent"    androID:ID="@+ID/vIEwPager"></androID.support.v4.vIEw.VIEwPager>

milIEu.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical" >    <TextVIEw        androID:ID="@+ID/textVIEw1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        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: main07-10 08:25:26.211: E/AndroIDRuntime(607): java.lang.NullPointerException07-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)

解决方法:

我发现您的代码有两个主要问题.

>查看v = inflater.inflate(R.layout.milIEu,container);

该容器应仅用于布局参数,而不能用于向其中添加展开视图.另外,您使用的inflate版本会返回容器本身.因此,您在错误的位置寻找视图.改变它

查看v = inflater.inflate(R.layout.milIEu,container,false);

> position是0索引的基础,因此,如果您在getCount中返回3,则将以position = 0,1,2调用getItem.这意味着您将为索引0返回null

总结

以上是内存溢出为你收集整理的java-具有Fragment和ViewPager的NullPointerException全部内容,希望文章能够帮你解决java-具有Fragment和ViewPager的NullPointerException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078235.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存