来自Activity的回调片段中的空指针异常

时间:2013-02-08 05:36:44

标签: android android-fragments nullpointerexception android-activity

在我们继续之前,我知道你在想什么。 我初始化了我的对象。我也用适当的方法初始化了(我试过onCreateView和onResume)。

我的问题是一个对象(实际上,不止一个对象正在抛出一个空指针,但是它们以相同的方式初始化),这就是抛出一个空指针异常,而对于我的生活,我不能为什么。

后台:我有一个包含TabHost和ViewPager的Activity。 Activity也是我拥有的Fragment的容器(我们称之为Frag A)。在我的活动中,当Frag A可见时,用户可以单击一个按钮,该按钮会显示两个连续的DialogFragments。当第二个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)

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

Null Pointer Exception来自TabsAdapter类中重写的 getItem 方法。问题是我的FragmentPagerAdapter实现没有保留对实例化的Fragment对象的引用/引用。

供将来参考:如果使用TabHost和ViewPager,添加一种方法来保持对PagerAdapter实例化片段的对象引用,以避免空指针异常。