我看到一个问题,即屏幕方向更改使正在开发的应用程序崩溃。
我正在使用Butterknife进行字段绑定,但是当更改方向时,我看到一个错误“未找到字段'xxxx'的ID为xxxxxx的必填视图'xxxx'”,这表明活动首次启动时的原始绑定发射已被摧毁。
我有BaseActivity.java:-
public abstract class BaseActivity extends AppCompatActivity {
private static final String TAG = "BaseActivity";
protected Global getGlobal() {
return (Global) getApplicationContext();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(getActivityView());
ButterKnife.bind(this);
}
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
abstract protected int getActivityView();
}
然后在MainActivity.java中,将上述基类扩展如下:-
public class MainActivity extends BaseActivity {
@BindView(R.id.main_menu_toolbar) protected Toolbar main_menu_toolbar;
@BindView(R.id.bottom_bar) protected BottomNavigationView bottom_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BottomNavigationViewHelper.disableShiftMode(bottom_bar);
bottom_bar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_home:
//TODO - reset scroll or pop fragment from stack..
break;
}
return true;
}
});
}
@Override
protected int getActivityView() {
return R.layout.activity_main;
}
}
调试错误如下
java.lang.RuntimeException: Unable to start activity ComponentInfo{aero.airborne.demo.qatar/aero.airborne.demo.qatar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.BottomNavigationView.getChildAt(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4919)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.BottomNavigationView.getChildAt(int)' on a null object reference
at aero.airborne.demo.qatar.helper.BottomNavigationViewHelper.disableShiftMode(BottomNavigationViewHelper.java:11)
at aero.airborne.demo.qatar.MainActivity.onCreate(MainActivity.java:64)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4919)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
有趣的是,如果我在MainActivity.java中删除了基类的依赖关系,以使MainActivity的onCreate()
执行setContentView()
,然后立即执行Butterknife.bind()
,它将起作用。这暗示了我的代码在Android生命周期中存在时间问题,涉及Butterknife何时为旧的方向解除绑定以及何时onCreate被称为vs bind(),但是到目前为止,我无法使其与我的Base类一起工作。
此时,任何想法或指示都将不胜感激。
答案 0 :(得分:1)
您仍然需要在方向改变时绑定Butterknife。将ButterKnife.bind(this);
从if语句中移出。