仅当构建变体处于发布模式时,才会在BottomSheetBehavior上获取NPE(NullPointerException)。如果我使用调试变体测试它工作正常。但当我将其作为发布版本时,应用程序崩溃了。我在onCreate()生命周期中调用setBottomSheet()方法。
这是我的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinator_scanactivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include layout="@layout/activity_scan_content" />
<!--<include layout="@layout/bottom_sheet" />-->
<RelativeLayout
android:id="@+id/bottom_sheet_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/white"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="35dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:id="@+id/bottom_sheet_header"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@color/colorPrimary"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:src="@drawable/bottom_sheet_up" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/bottom_sheet_header"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp" />
</RelativeLayout>
<ImageView
android:id="@+id/add_device_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/divider_view"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:src="@drawable/add"
app:layout_anchor="@id/bottom_sheet"
app:layout_anchorGravity="top|end" />
</android.support.design.widget.CoordinatorLayout>
这是我的java代码:
private void setBottomSheet() {
llBottomSheet = (RelativeLayout) findViewById(R.id.bottom_sheet_layout);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
System.out.println("Inactive Device Bottom Sheet Expanded");
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
add_device_btn.animate().scaleX(1 - slideOffset).scaleY(1 - slideOffset).setDuration(0).start();
}
});
}
我的日志文件:
FATAL EXCEPTION: main
Process: com.webmobi.trackm, PID: 2676
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.ViewDragHelper.processTouchEvent(android.view.MotionEvent)' on a null object reference
at android.support.design.widget.BottomSheetBehavior.onTouchEvent(BottomSheetBehavior.java:315)
at android.support.design.widget.CoordinatorLayout.resetTouchBehaviors(CoordinatorLayout.java:396)
at android.support.design.widget.CoordinatorLayout.onAttachedToWindow(CoordinatorLayout.java:238)
at android.view.View.dispatchAttachedToWindow(View.java:15532)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2918)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2925)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2925)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2925)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2925)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2925)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1572)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1300)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6405)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:876)
at android.view.Choreographer.doCallbacks(Choreographer.java:688)
at android.view.Choreographer.doFrame(Choreographer.java:623)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:862)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6205)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
答案 0 :(得分:0)
添加此代码:
if (mViewDragHelper == null) {
mViewDragHelper = ViewDragHelper.create(parent, mDragCallback);
}
在mViewDragHelper.processTouchEvent(event);
之前进入 ViewPagerBottomSheetBehavior 文件(大概可能是274行)。