I recently started using the DrawerLayout in a new project about a week ago and it\'s all working fine. Apart from the odd times when swiping it in and out with my finger it
I think I've managed to fix this by creating my own copies of the files I needed from the support library source code and catching the null View in isContentView()
.
The files I needed from the source were:
I put them in their own package in my project and renamed DrawerLayout
to FixedDrawerLayout
. (So I can easily tell which copy of the DrawerLayout I'm using)
I then changed isContentView()
to:
boolean isContentView(View child) {
if(child == null){
return false;
}
return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
}
After swapping all references to the DrawerLayout
in my Activity
to FixedDrawerLayout
it seems to have fixed it.
Not sure if there is anything that can happen by just returning false
in isContentView()
, but it doesn't crash anymore and it's still usable even when child
is null.
Building on Mike's answer and user1050133's comment:
1) Create the package android.support.v4.widget
in your app
2) Add a new file called FixedDrawerLayout
with the following content:
public class FixedDrawerLayout extends DrawerLayout {
public FixedDrawerLayout(Context context) {
super(context);
}
public FixedDrawerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FixedDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
boolean isContentView(View child) {
if (child == null) {
return false;
}
return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
}
}
3) Replace all uses of DrawerLayout in xml with FixedDrawerLayout
I had the same issue while using the navigation drawer, and I have solved the problem simply by removing the android:focusableInTouchMode="true"
from the root layout of the fragment.
I have also got this error message but at first I couldn't find out what caused it. After doing some tests I actually found out that removing all margins on the View (in your example the fragment) seems to remove the problem.
So in your case it would be:
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_list"
android:name="com.navdrawertest.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemListActivity"
tools:layout="@android:layout/list_content" />
and you can set margins on all the views inside this fragment instead (but I haven't tested it). Hope it helps someone. It seems that this problem is not so common.
I faced with similar problem.
I put a fake onclick mathod for fragment layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f1f2f6"
android:onClick="fragmentClick"
android:orientation="vertical" >
I put the onclick method in activity java code just for fake.
public void fragmentClick(View v){
Log.i("fragmentClick", "fragmentClick");
return;
}
Problem was solved.