意外生命周期导致致命异常?

时间:2015-04-18 03:36:37

标签: android nullpointerexception crash roboguice

我是iOS开发者,完全没有使用过Android的经验,所以如果我难以表达我的问题,请原谅我:

我在Android应用中遇到致命异常我负责维护。

Fatal Exception: java.lang.RuntimeException Unable to start activity ComponentInfo{…}: java.lang.NullPointerException

我可以从崩溃报告(下面附件)中看到,崩溃发生在我的一个片段中,名为" bindLocation"

以下是它崩溃的路线:

            mLocationHeaderTextView.setVisibility(View.VISIBLE);

很明显,问题是mLocationHeaderTextview为null。使用roboguice注入mLocationHeaderTextView,如下所示:

 @InjectView(R.id.filter_location_header)
TextView mLocationHeaderTextView;

现在,我认为错误是由于我的textView没有被注入'。在查看堆栈跟踪之后,我确定这可能是在调用bindLocation()之前未调用onViewCreated()的结果。

然而,这是我迷路的地方。我对android非常不熟悉,所以我不确定我的onViewCreated方法是如何被跳过的。我已经在设备上尝试了一百万件事来尝试重现这种情况,但我无法做到。无论我怎么想尝试用户操作,我的onViewCreated方法都会被调用,而我的变量是非空的。也许我误读了堆栈跟踪。我希望下面的堆栈跟踪提供足够的信息,以便所有人帮助我,但如果没有,请告诉我还能告诉你关于该应用的其他信息。我无法发布我的完整源代码(因为它不属于我),但我会提供任何我能提供的信息。

非常感谢你的帮助!

enter image description here

来自我的FilterDrawerFragment:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_filter_drawer, container, false);

    ButterKnife.inject(this, view);

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mRulesHeaderReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            resetRules();
        }
    });

    if (getActivity() != null && mDrawerLayout == null) {
        mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.filter_drawer_layout);

        if (mDrawerLayout != null) {
            mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
                @Override
                public void onDrawerSlide(View view, float v) {

                }

                @Override
                public void onDrawerOpened(View view) {
                    NavigationUtils.invalidateOptionsMenu(getActivity());
                }

                @Override
                public void onDrawerClosed(View view) {
                    notifyFiltersChanged();

                    NavigationUtils.invalidateOptionsMenu(getActivity());
                }

                @Override
                public void onDrawerStateChanged(int i) {

                }
            });

            // set a custom shadow that overlays the main content when the drawer opens
            mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.END);
        }
    }

    mlocationChangeButton.setOnClickListener(this);

    bindUI();
}

 private void bindUI() {
    if (isAdded() && mConfiguration != null) {

        // bind the location
        bindLocation();
        …
    }
}

private void bindLocation() {
    if (isAdded() && mConfiguration != null && mConfiguration.isLocationEnabled()) {

        // show the location related items
        mLocationHeaderTextView.setVisibility(View.VISIBLE);//Crash reported here.
        …
            }
        });

    }
}


/**
 * Users of this fragment must call this to update the filter configuration
 */
public void updateConfiguration(FilterConfiguration configuration) {
    if (configuration != null && !configuration.equals(mConfiguration)) {
        mConfiguration = configuration;

        bindUI();
    }
}

从中调用updateConfiguration() LocationFinderFragment:

 @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (getArguments() != null) {
          ………
    if (savedInstanceState == null) {
        mFilterDrawerFragment = FilterDrawerFragment.newInstance();
        getChildFragmentManager()
                .beginTransaction()
                .replace(R.id.filter_drawer, mFilterDrawerFragment)
                .commit();

    } else {
        mFilterDrawerFragment = (FilterDrawerFragment) getChildFragmentManager().findFragmentById(R.id.filter_drawer);
    }

    ………         
);
    …………………
    populateFilters();
}


private void populateFilters() {
    // these might be passed into the fragment
    if (mFacets != null) {
        FilterConfiguration config = new FilterConfiguration() {{
        ……………………
        }};

        mFilterDrawerFragment.updateConfiguration(config);

2 个答案:

答案 0 :(得分:0)

现在我觉得我看到了问题。

  1. 在roboguice视图中注入了onViewCreated()
  2. onViewCreated()之后调用
  3. onCreateView()
  4. bindLocation()中调用
  5. onCreateView()
  6. 因此,到目前为止,还没有通过roboguice注入观点。

    请致电bindLocation()后在onViewCreated()致电super

答案 1 :(得分:0)

这就是我解决这个问题的方法。当你使用ButterKnife时。 没有这些配置ButterKnife会抛出NPE

在使用此库之前,您必须对ButterKnife Eclipse Configuration进行一些配置 ButterKnife Android Studio Configuration

如果您正在使用ADT并且缺少Annototion处理,请选中此项以安装Android Eclipse - Cannot see annotation processing option

相关问题