Android Studio setSupportActionBar无效

时间:2015-09-20 04:12:06

标签: java android xml

我正在尝试使用Android Studio制作完整的标题应用。我有MainActivity.java,navheader.xml,cardview.xml,activity_detail.xml。当我键入代码并构建代码时,没有发现错误。

当应用程序运行时,MainActivity.java中存在错误。

错误说:

java.lang.RuntimeException: Unable to start activity ComponentInfo{id.wdharmana.doahindu/id.wdharmana.doahindu.MainActivity}: java.lang.NullPointerException

    at id.wdharmana.doahindu.MainActivity.onCreate(MainActivity.java:42)

这是我的MainActivity.java:

     private CollapsingToolbarLayout ctb;
    private int mutedColor;

    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private Snackbar snackbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* Top toolbar */
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

       /* Bottom toolbar. */
        Toolbar bottomToolbar = (Toolbar) findViewById(R.id.bottom_toolbar);
        bottomToolbar.inflateMenu(R.menu.menu_main);
        bottomToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                String message = null;
                if (item.getItemId() == R.id.action_copy){
                    message = "Copy clicked";
                }else if (item.getItemId() == R.id.action_cut){
                    message = "Cut clicked";
                }else if (item.getItemId() == R.id.action_record){
                    message = "Record clicked";
                }

                Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
                return true;
            }
        });

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // Add 8 cards
        MyAdapter adapter = new MyAdapter(new String[8]);
        recyclerView.setAdapter(adapter);

        ctb = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
///* Define the image */
//        ImageView image = (ImageView) findViewById(R.id.image);
///* Decode bitmap from the image */
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
//                R.drawable.full_image);
///* Generate palette from the image bitmap */
//        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
//            @Override
//            public void onGenerated(Palette palette) {
//                    mutedColor = palette.getMutedColor
//                            (ContextCompat.getColor(MainActivity.this,
//                                    R.color.primary));
//                      /* Set toolbar color from the palette */
//                ctb.setContentScrimColor(mutedColor);
//            }
//        });
        ctb.setContentScrimColor(ContextCompat.getColor(MainActivity.this, R.color.primary));
        ctb.setTitle(getString(R.string.app_name));

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);

        // On click of menu icon on toolbar
        toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

// On click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                // Set item in checked state
                menuItem.setChecked(true);

//TODO: handle navigation

//Closing drawer on item click
                drawerLayout.closeDrawers();
                return true;
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Do something awesome?",
                        Snackbar.LENGTH_LONG).show();
            }
        });


    }

    /* Create RecylcerView Adapter. */
    public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private String[] mDataset;

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public View view;
            public TextView title;
            public ViewHolder(View v) {
                super(v);
                view = v;
                title = (TextView) v.findViewById(R.id.card_title);
            }
        }

        public MyAdapter(String[] myDataset) {
            mDataset = myDataset;
        }

        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View cardview = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.cardview, parent, false);
            return new ViewHolder(cardview);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.title.setText("Card " + (position + 1));
        }

        @Override
        public int getItemCount() {
            return mDataset.length;
        }
    }

Activity_detail.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"  tools:context=".MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="?attr/actionBarSize">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="192dp">
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:theme="@style/ThemeOverlay.AppCompat.Dark"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/full_image"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:layout_collapseMode="pin" />
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:clipToPadding="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp"
            android:layout_gravity="right|bottom"
            android:tint="@android:color/white"
            android:src="@drawable/ic_add"/>


    </android.support.design.widget.CoordinatorLayout>

    <android.support.v7.widget.Toolbar
        android:id="@+id/bottom_toolbar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
        android:background="?attr/colorPrimaryDark"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#ffffff"
    android:clickable="true"
    app:headerLayout="@layout/navheader"
    app:menu="@menu/menu_navigation"
    app:itemBackground="?attr/selectableItemBackground"/>

Card_View.xml

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
card_view:contentPadding="8dp"
android:clickable="true"
android:foreground="?attr/selectableItemBackground">
<TextView
    android:id="@+id/card_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@style/TextAppearance.AppCompat.Title"
    tools:text="Title text"/>

NavHeader.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="222dp"
android:background="?attr/colorPrimaryDark"
android:padding="16dp"
android:orientation="vertical">

感谢您的关注和提前感谢!

0 个答案:

没有答案