在我们继续之前,我知道你在想什么。我初始化了我的对象。我还在适当的方法中进行了初始化(我尝试了 onCreateView 和 onResume)。
我的问题是一个抛出空指针异常的对象(实际上,不止一个对象抛出空指针,但它们以相同的方式初始化),我终其一生都无法弄清楚原因。
背景:我有一个包含 TabHost 和 ViewPager 的 Activity。Activity 也是我拥有的 Fragment 的容器(我们将其称为 Frag A)。在我的 Activity 中,当 Frag A 可见时,用户可以单击一个按钮,该按钮会显示两个连续的 DialogFragment。当第二个 DialogFragment 完成时,它从 Frag A 调用一个方法,将一些视图添加到 Frag A 的布局。调用该方法时,尽管我在 onCreateView 和 onResume 中都尝试这样做,但我的所有对象都没有保持初始化状态。
以下是适用的代码: 执行该行时在最后一个方法中引发异常wpp.addBlankExercise();
。我 99% 确信这与 TabHost/ViewPager 在 Frag A 上使用生命周期回调的方式有关。我不知道 TabHost/ViewPager 的哪个部分导致了这种情况。
活动
static class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;
public DummyTabFactory(Context context) {
mContext = context;
}
@Override
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mContext));
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
mTabs.add(info);
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public void onTabChanged(String tabId) {
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
@Override
public void onPageSelected(int position) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
}
@Override
public void getChoiceIndex(DialogFragment dialog) {
amount = QuantityDialogFragment.amount;
/* We keep track of which exercises are cardio exercises and which exercises are strength exercises here. If it is a cardio
* exercise then the value is true (first choice) if it's not then the value is false (second example). */
if (result == 0) {
// Cardio exercise
wpp.cardioBoolean = true;
/* Null Pointer thrown here because the global variables that "addBlankExercise()" uses are no longer initialized. */
wpp.addBlankExercise();
} else {
// Strength exercise
// Null Pointer also thrown here for the same reason.
wpp.cardioBoolean = false;
wpp.addBlankExercise();
}
}
这里是一小部分addBlankExercise()
。它比这长得多,但它使用这里看到的基本模式:
public void addBlankExercise () {
printLineNumber();
System.out.println("addBlankExercise() " + getLineNumber());
for (int i = 0; i < WorkoutPlannerActivity.amount; i++) {
printLineNumber();
if (isCardioList.size() == etExerciseTracker) {
isCardioList.add(cardioBoolean);
}
printLineNumber();
LinearLayout layoutInfo = new LinearLayout(context);
layoutInfo.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layoutInfo.setOrientation(LinearLayout.HORIZONTAL);
layoutInfo.setBackgroundColor(colorBlack);
layoutInfo.setPadding(0, 0, 10, 5);
editTextTitle = new EditText(context);
editTextTitle.setHint("Sample Exercise");
editTextTitle.setTextAppearance(context, R.style.style_exercise_title);
editTextTitle.setHintTextColor(colorRed);
editTextTitle.setBackgroundColor(colorAndroidTransparent);
editTextTitle.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 12));
editTextTitle.setTag(editTextTitle.getKeyListener());
editTextTitle.setId(etExerciseTracker);
etExerciseTracker ++;
这是堆栈跟踪:
02-08 07:26:46.082: E/AndroidRuntime(6790): FATAL EXCEPTION: main
02-08 07:26:46.082: E/AndroidRuntime(6790): java.lang.NullPointerException
02-08 07:26:46.082: E/AndroidRuntime(6790): at android.view.ViewConfiguration.get(ViewConfiguration.java:318)
02-08 07:26:46.082: E/AndroidRuntime(6790): at android.view.View.<init>(View.java:3234)
02-08 07:26:46.082: E/AndroidRuntime(6790): at android.view.ViewGroup.<init> (ViewGroup.java:420)
02-08 07:26:46.082: E/AndroidRuntime(6790): at android.widget.LinearLayout.<init> (LinearLayout.java:168)
02-08 07:26:46.082: E/AndroidRuntime(6790): at com.nutrifit.Fragments.WorkoutPlannerPopup.addBlankExercise(WorkoutPlannerPopup.java:398)
02-08 07:26:46.082: E/AndroidRuntime(6790): at com.nutrifit1.Activities.WorkoutPlannerActivity.getChoiceIndex(WorkoutPlannerActivity.java:198)
02-08 07:26:46.082: E/AndroidRuntime(6790): at com.nutrifit.Fragments.QuantityDialogFragment$1.onClick(QuantityDialogFragment.java:44)
非常感谢您的帮助!