【发布时间】:2016-10-11 00:05:03
【问题描述】:
我关注了ViewPager FragmentPagerAdapter Nullpointer,但没有任何帮助。任何解决方案将不胜感激。请注意,我只从支持库中导入了类。
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.v4.view.ViewPager.OnPageChangeListener;
这是我在 Splunk Mint 中收到的崩溃日志
java.lang.NullPointerException 在 android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:416) 在 android.support.v4.app.BackStackRecord.add(BackStackRecord.java:411) 在 android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99) 在 android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837) 在 android.support.v4.view.ViewPager.populate(ViewPager.java:987) 在 android.support.v4.view.ViewPager.populate(ViewPager.java:919) 在 android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441) 在 android.view.View.measure(View.java:15575) 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:833) 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 在 android.view.View.measure(View.java:15575) 在 android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617) 在 android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399) 在 android.view.View.measure(View.java:15575) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在 android.view.View.measure(View.java:15575) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396) 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:681) 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 在 android.view.View.measure(View.java:15575) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在 com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423) 在 android.view.View.measure(View.java:15575) 在 android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031) 在 android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249) 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1425) 在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1142) 在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4724) 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 在 android.view.Choreographer.doCallbacks(Choreographer.java:555) 在 android.view.Choreographer.doFrame(Choreographer.java:525) 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 在 android.os.Handler.handleCallback(Handler.java:615) 在 android.os.Handler.dispatchMessage(Handler.java:92) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:4947) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 在 dalvik.system.NativeStart.main(Native Method)
这是我的 FragmentPagerAdapter 的代码。
private class MyPageAdapter extends FragmentPagerAdapter {
/**
* Instantiates a new my page adapter.
*
* @param fm the fm
*/
public MyPageAdapter(FragmentManager fm) {
super(fm);
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
try {
TypedArray a = null, b = null;
int attributeResourceId = 0, attributeStepsId = 0;
switch (currentLocale.toString().trim()) {
// if (currentLocale.toString().trim().equals("ar")) {
case "ar":
if (position == 0) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard2 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step1 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard2 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step1 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step1_title, R.string.activity_wizard_help_step2_line2, attributeStepsId);
} else if (position == 1) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step2 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step2 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step2_title, R.string.activity_wizard_help_step1_line2, attributeStepsId);
}
else if (position == 2) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard3_ar });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step3 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard3_ar });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step3 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step3_title, R.string.activity_wizard_help_step3_line2, attributeStepsId);
}
else if (position == 3) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard4_ar });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step4 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard4_ar });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step4 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step4_title, R.string.activity_wizard_help_step4_line2, attributeStepsId);
}
else {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step1 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step1 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step1_title, R.string.activity_wizard_help_step1_line2, attributeStepsId);
}
// } else {
break;
case "en":
if (position == 0) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard2 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step1 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard2 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step1 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step1_title, R.string.activity_wizard_help_step2_line2, attributeStepsId);
} else if (position == 1) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step2 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step2 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step2_title, R.string.activity_wizard_help_step1_line2, attributeStepsId);
}
else if (position == 2) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard3 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step3 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard3 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step3 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step3_title, R.string.activity_wizard_help_step3_line2, attributeStepsId);
}
else if (position == 3) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard4 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step4 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light,
new int[] { R.attr.wizard4 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step4 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step4_title, R.string.activity_wizard_help_step4_line2, attributeStepsId);
}
else {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step1 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step1 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step1_title, R.string.activity_wizard_help_step1_line2, attributeStepsId);
}
break;
case "hi":
if (position == 0) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard2 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step1 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard2 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step1 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step1_title, R.string.activity_wizard_help_step2_line2, attributeStepsId);
} else if (position == 1) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step2 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step2 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step2_title, R.string.activity_wizard_help_step1_line2, attributeStepsId);
}
else if (position == 2) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard3_hi });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step3 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard3_hi });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step3 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step3_title, R.string.activity_wizard_help_step3_line2, attributeStepsId);
}
else if (position == 3) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard4_hi });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step4 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard4_hi });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step4 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step4_title, R.string.activity_wizard_help_step4_line2, attributeStepsId);
}
else {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step1 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step1 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step1_title, R.string.activity_wizard_help_step1_line2, attributeStepsId);
}
break;
case "ur":
if (position == 0) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard2 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step1 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard2 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step1 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step1_title, R.string.activity_wizard_help_step2_line2, attributeStepsId);
} else if (position == 1) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step2 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step2 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step2_title, R.string.activity_wizard_help_step1_line2, attributeStepsId);
}
else if (position == 2) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard3_ur });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step3 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard3_ur });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step3 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step3_title, R.string.activity_wizard_help_step3_line2, attributeStepsId);
}
else if (position == 3) {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard4_ur });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step4 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard4_ur });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step4 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step4_title, R.string.activity_wizard_help_step4_line2, attributeStepsId);
}
else {
if (Util.getSharedPref(WizardHelpActivity.this).getInt("Theme", ConstantData.AppTheme.Dark.getValue()) == ConstantData.AppTheme.Dark.getValue()) {
a = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Dark, new int[] { R.attr.step1 });
} else {
a = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.wizard1 });
b = getTheme().obtainStyledAttributes(R.style.Light, new int[] { R.attr.step1 });
}
attributeResourceId = a.getResourceId(0, 0);
attributeStepsId = b.getResourceId(0, 0);
a.recycle();
fragment = WizardHelpPageFragment.getInstance(attributeResourceId, R.string.wizard_help_step1_title, R.string.activity_wizard_help_step1_line2, attributeStepsId);
}
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
new SendEmailAsyncTask(getApplicationContext(), getClass().getName(),"getItem(int position)", e).execute();
}
return fragment;
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return 4;
}
}
【问题讨论】:
-
来自您链接的问题的提示:“非常仔细地检查您的
getItem(int index)方法,并寻找任何可能导致您最终得到空片段的逻辑、场景或丢失的 'break' 语句。”在异常情况下,您将返回空值。 -
@jankigadhiya 没有您指定的此类。
标签: android