无法关闭项目上的抽屉点击

时间:2017-02-24 01:23:48

标签: android navigation-drawer android-viewholder

以下代码工作正常,但下面标记的标记部分除外:

public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.MyViewHolder> {

    private List<NavigationDrawerItem> mDataList = Collections.emptyList();
    private LayoutInflater inflater;
    private Context context;

public NavigationDrawerAdapter(Context context, List<NavigationDrawerItem> data) {
    this.context = context;
    inflater = LayoutInflater.from(context);
    this.mDataList = data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.nav_drawer_list_item, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    NavigationDrawerItem current = mDataList.get(position);

    holder.imgIcon.setImageResource(current.getImageId());
    holder.title.setText(current.getTitle());
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(context, holder.title.getText().toString(), Toast.LENGTH_SHORT).show();



        Fragment fragment = new PurchaseFragment();
            FragmentTransaction ft = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment);
            ft.commit();

            //Close Drawer
            DrawerLayout mDrawerLayout;
            mDrawerLayout = (DrawerLayout) v.findViewById(R.id.drawer_layout);
            mDrawerLayout.closeDrawers();
        }
    });
}

@Override
public int getItemCount() {
    return mDataList.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {
    TextView title;
    ImageView imgIcon;

    public MyViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.title);
        imgIcon = (ImageView) itemView.findViewById(R.id.imgIcon);
    }
}

}

错误:

//Close Drawer
DrawerLayout mDrawerLayout;
mDrawerLayout = (DrawerLayout) v.findViewById(R.id.drawer_layout);
                mDrawerLayout.closeDrawers();

显示空指针异常。

在Android Studio,调试器模式下,变量v显示:

v = {android.widget.LinearLayout@831899121384} "android.widget.LinearLayout{b10d36e8 V.E...C. .......P 0,0-280,48}"

mDrawerLayout未获得抽屉布局。它正在获得我当前View&#34;的LinearLayout。所以当点击项目时我无法关闭抽屉。

这里有什么问题?

崩溃日志

02-23 20:32:13.528 3834-3834/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: ismail.proximasoft.com.hisab, PID: 3834
java.lang.NullPointerException
at ismail.proximasoft.com.hisab.adapter.NavigationDrawerAdapter$1.onClick(NavigationDrawerAdapter.java:64)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

activity_main.xml中

<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <fragment
        android:id="@+id/nav_drwr_fragment"
        android:name="ismail.proximasoft.com.hisab.app.NavigationDrawerFragment"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

</LinearLayout>

nav_drawer_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="48dp"
             android:background="?attr/selectableItemBackground"
             android:clickable="true"
             android:gravity="center"
             android:orientation="horizontal">

<ImageView
    android:id="@+id/imgIcon"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:src="@drawable/ic_discard"
    android:tint="@color/grey_500"/>

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:textColor="@android:color/black"
    android:textSize="14sp"/>

</LinearLayout>

0 个答案:

没有答案