当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Android app crashes when starting it: issue with toolbar

问题描述:

I am trying to learn how to develop android apps by reading the book "Learn android Studio" by Adam Gerber and Clifton Craig. This question has been asked a lot and I've been trying to find a solution for 2 days now but I just can't fix it since I am fairly new to programming.

I am trying to build an app that allows for the creation of 1 line reminders. From what I understood, the book was written when the actionBar was still a thing in Android Studio so I am trying to figure out how to make my Toolbar appear with the new Android Studio version. I have the following in my RemindersActivity.

package com.apress.gerber.reminders;

import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import java.util.List;

public class RemindersActivity extends AppCompatActivity {

private ListView mListView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.content_reminders);

mListView = (ListView) findViewById(R.id.reminders_list_view);

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(

this, R.layout.reminders_row, R.id.row_text,

new String[]{"first record", "second record", "third record"});

mListView.setAdapter(arrayAdapter);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

.setAction("Action", null).show();

}

});

}

}

When I comment out the line setSupportActionBar(toolbar) and the remainder of my code the app runs fine but I can't see my action bar. However, when I leave everything the way it is the app crashes and doesn't run. Here is my Styles.xml file if that may help:

<resources>

<!-- Base application theme. -->

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<!-- Customize your theme here. -->

<item name="colorPrimary">@color/colorPrimary</item>

<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

<item name="colorAccent">@color/colorAccent</item>

</style>

<style name="AppTheme.NoActionBar">

<item name="windowActionBar">false</item>

<item name="windowNoTitle">true</item>

</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

and my activity_reminders file:

<?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:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

tools:context="com.apress.gerber.reminders.RemindersActivity">

<android.support.design.widget.AppBarLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

<android.support.design.widget.FloatingActionButton

android:id="@+id/fab"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom|end"

android:layout_margin="@dimen/fab_margin"

android:src="@android:drawable/ic_dialog_email"/>

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

And finally here is my androidManifest.xml file. I am running api 23 with build tools 23.

<?xml version="1.0" encoding="utf-8"?>

<manifest package="com.apress.gerber.reminders"

xmlns:android="http://schemas.android.com/apk/res/android">

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity

android:name=".RemindersActivity"

android:label="@string/app_name"

android:theme="@style/AppTheme.NoActionBar">

<intent-filter>

<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

</application>

</manifest>

网友答案:

Use this code in your class file to attach toolbar to your activity

 toolbar = (Toolbar) findViewById(R.id.toolbar_screen_ads); // Attaching the layout to the toolbar object
    setSupportActionBar(toolbar);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setElevation(2);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(true);

    toolbar.setContentInsetsAbsolute(0, 0);

also make changes in your style

     <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/colorGray</item>
</style>
网友答案:
<item name="windowActionBar">false</item>

Set it to true

分享给朋友:
您可能感兴趣的文章:
随机阅读: