2015-12-21 28 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