我的代码有问题.我在下面的代码中有一个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: 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)
解决方法:
我发现您的代码有两个主要问题.
>查看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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。