我是一個iOS開發人員,絕對沒有Android的經驗,所以原諒我,如果我有困難表達我的問題:意外生命週期導致致命異常?
我得到一個致命的異常,我負責維護一個Android應用程序。
Fatal Exception: java.lang.RuntimeException Unable to start activity ComponentInfo{…}: java.lang.NullPointerException
我可以從崩潰報告(附後)認爲,墜機是發生在我的一個片段在一個名爲「bindLocation」方法
這裏是它的崩潰上線看到:
mLocationHeaderTextView.setVisibility(View.VISIBLE);
顯然問題是mLocationHeaderTextview爲null。
@InjectView(R.id.filter_location_header)
TextView mLocationHeaderTextView;
現在,我認爲錯誤是不是我的TextView造成「注入」:mLocationHeaderTextView使用roboguice如下注入。在查看堆棧跟蹤之後,我確定這可能是onViewCreated()在調用bindLocation()之前未被調用的結果。
然而,這是我迷路的地方。我對android非常陌生,所以我不確定我的onViewCreated方法可能被跳過了。我在設備上嘗試了一百萬次嘗試重現這種情況,但我無法完成。無論用戶採取什麼行動,我都可以嘗試,我的onViewCreated方法被調用,而我的變量是非空的。也許我誤解了堆棧跟蹤。我希望我的堆棧跟蹤提供了足夠的信息讓你們都能幫助我,但是如果沒有,請告訴我還有什麼可以告訴你的應用程序。我無法發佈我的完整源代碼(因爲它不屬於我),但我會提供我所能提供的任何信息。
非常感謝您的幫助!
從我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);
嘗試if(mLocationHeaderTextView!= null)mLocationHeaderTextView.setVisibility(View.VISIBLE);讓我知道如果解決這個問題。 –
我知道如何檢查空變量,但我沒有真的試圖解決這個變量爲空的單純事實,我試圖理解是什麼導致它爲空。 –
我假設它在試圖保留狀態時崩潰了? –