2015-12-21 17 views
-1

我想通过阅读Adam Gerber和Clifton Craig的书“Learn android Studio”来学习如何开发android应用程序。这个问题已经被问了很多,我一直试图找到一个解决方案2天,但我无法修复它,因为我对编程相当陌生。Android应用程序在启动时崩溃:问题与工具栏

我想构建一个允许创建1行提醒的应用程序。根据我的理解,这本书是在ActionBar仍然是Android Studio中的一部分时编写的,因此我正试图弄清楚如何使我的工具栏出现在新的Android Studio版本中。我在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(); 
      } 
     }); 
    } 
} 

当我注释掉线setSupportActionBar(工具栏)和我的代码的应用程序运行正常,其余但我不能看我的行动吧。然而,当我离开一切的方式,它是应用程序崩溃,并没有运行。这是我的Styles.xml文件,如果可以帮助:

<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> 

和我activity_reminders文件:

<?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> 

最后这里是我的AndroidManifest.xml文件。我正在运行的API 23的构建工具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> 
+0

你能粘贴崩溃日志以及.. – prijupaul

+0

从工具栏删除您的应用程序:popupTheme。 –

+0

您遗漏了最重要的信息之一 - logcat输出。 – csmckelvey

回答

1

使用此代码在类文件到工具栏安装到你的活动

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); 

也使改变你的风格

 <!-- 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> 
0
<item name="windowActionBar">false</item> 

将其设置为true

+1

解释你的答案。 [来自评论](http://stackoverflow.com/review/low-quality-posts/10634472) – Alex