2015-08-15 157 views
0

嗨,大家好,我已經面對這個問題幾天了。我用google搜索了一下,但沒有人對這個問題有精確的解決方案。setSupportActionBar(工具欄工具欄)不工作在我的材料項目

我是一個初學者在android的材料設計。我想創建我的應用程序工具欄,使用setSupportActionBar(Toolbar toolbar),應用程序崩潰,當我擺脫setSupportActionBar(Toolbar toolbar)運行應用程序並不會崩潰

這是我的代碼,但是一旦..

的build.gradle (模塊:APP)

`apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 22 
    buildToolsVersion "23.0.0 rc3" 

    defaultConfig { 
     applicationId "com.example.benson.bestqsystems" 
     minSdkVersion 9 
     targetSdkVersion 22 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:22.2.1' 
} 
` 

styles.xml

`<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="AppTheme.Base"> 
     <!-- Customize your theme here. --> 
    </style> 

    <style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 
</resources> 
` 

style.xml爲V21

`<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme" parent="AppTheme.Base"> 
     <item name="android:colorPrimary">@color/colorPrimary</item> 
     <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="android:colorAccent">@color/colorAccent</item> 
    </style> 
</resources> 
` 

my_app_bar.xml(包含我的工具欄佈局)

`<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:id="@+id/app_bar" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

</android.support.v7.widget.Toolbar>` 

main_activity佈局:

`<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include layout="@layout/my_app_bar" 

     /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:padding="10dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="35sp" /> 

    <Button 
     android:id="@+id/testing" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="move to second acitivity" /> 
</LinearLayout> 
` 

MainActivity.java:

`package com.example.benson.bestqsystems; 

import android.content.Intent; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 

import java.security.MessageDigestSpi; 

public class MainAcitvity extends AppCompatActivity implements View.OnClickListener{ 
    private Button move; 
    private Toolbar toolbar; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_acitvity); 
     move = (Button) findViewById(R.id.testing); 
     move.setOnClickListener(this); 
     setSupportActionBar(toolbar); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main_acitvity, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onClick(View v) { 
     switch(v.getId()){ 
      case R.id.testing: 
       Message.message(this, "testing please"); 
       startActivity(new Intent(this, Second.class)); 
       break; 
     } 
    } 
} 
` 

AndroidManifest.xml中

`<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.benson.bestqsystems" > 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainAcitvity" 
      android:label="@string/title_activity_main_acitvity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Second" 
      android:label="@string/title_activity_second" > 
     </activity> 
    </application> 

</manifest> 
` 

運行我的應用程序後,這是我的logcat

`java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.benson.bestqsystems/com.example.benson.bestqsystems.MainAcitvity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
      at android.app.ActivityThread.access$600(ActivityThread.java:123) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4424) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:94) 
      at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:87) 
      at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:77) 
      at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198) 
      at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:96) 
      at com.example.benson.bestqsystems.MainAcitvity.onCreate(MainAcitvity.java:24) 
      at android.app.Activity.performCreate(Activity.java:4465) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
            at android.app.ActivityThread.access$600(ActivityThread.java:123) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4424) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592) 
            at dalvik.system.NativeStart.main(Native Method)` 

任何人的幫助,我會感謝這麼多。

回答

1

因爲在使用它之前你還沒有初始化toolbar,所以你只需要添加它。

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

在此之前:

setSupportActionBar(toolbar);

+0

哈哈,這是被初學者問題。我們總是對小事感到興奮......非常感謝..它已經奏效......美好的時光和熱情的問候...... –

+0

每個人都可以犯簡單的錯誤) –