11

Ich habe festgefahren, überprüft die offizielle Anleitung usw. Alle Tutorials/Was sind die Schritte, um von ActionBar zu ActionBarCompat (für die Toolbar und Unterstützung von älteren Versionen? Ich habe appcompat-v7: 21.0. +, versuchteAndroid: Wie/Tutorial zum Ändern von ActionBar zu ActionBarCompat (Toolbar)?

getSupportActionBar(); und

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

setSupportActionBar(toolbar); 

verändertes Thema Thema in Stilen zu AppCompat ... Irgendwelche häufigen Fehler oder Ideen beachten

Bewahren Sie diese Art von Störung zu erhalten:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.battery.plusfree/com.battery.plusfree.MainCollectionActivity}: java.lang.NullPointerException 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) 
     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5146) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 
     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
     at com.battery.plusfree.MainCollectionActivity.onCreate(MainCollectionActivity.java:133) 
     at android.app.Activity.performCreate(Activity.java:5231) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5146) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 
            at dalvik.system.NativeStart.main(Native Method) 
+1

Ein guter Anfang: https: //github.com/kanytu/android-material-drawer-template –

+0

post ur MainColectionActivity –

+1

Veröffentlichen Sie Ihre Aktivität und Layout. Das muss über Toolbar lesen: http://android-developers.blogspot.it/2014/10/appcompat-v21-material-design-for-pre.html –

Antwort

13

Ich habe mit meiner Anwendung gelernt, dass Sie die Symbolleiste und die Aktionsleiste nicht gleichzeitig verwenden können. Also das könnte, wo das Problem liegt, aber ich bin mir nicht sicher, weil Sie nicht Ihren Code schreiben, aber mit der styles.xml Datei Sie entweder haben oder wie so:

<style name="AppTheme.Base" parent="Theme.AppCompat"> 
    <item name="colorPrimary">@color/mycolorprimary</item> 
    <item name="colorPrimaryDark">@color/mycolorprimarydark</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="windowActionBar">false</item> 
</style> 

Activity.xml :

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

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

    <!-- dont use this if you only want to use the actionbar instead --> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:elevation="5dp" 
     android:minHeight="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" /> 

</LinearLayout> 

Activity.java:

public class MyActivity extends ActionBarActivity { 

     Toolbar mToolbar; 

     public void onCreate(Bundle savedInstanceState) { 

      // configure toolbar stuff 
      setSupportActionBar(mToolbar); 

      // or if you don't want to use the toolbar 
      // then change the style values accordingly 
      // and then you can run getSupportActionBar() instead 

     } 

    } 

Referenz: here .. hoffe, das hilft!

+0

Fehler wird angezeigt. setSupportActionBar zeigt nicht an, wenn ctrl + space im Fragment ist. –

+0

@KarthikeyanVe-Fragmente haben weder eine 'ActionBar' noch eine' Toolbar'. Ich nehme an, Sie haben es bereits über die einfache Google-Suche gefunden. – Sufian

4

@ Andrew Butler: gute Antwort, obwohl es eine Standardmethode gibt, eine Aktionsleiste nicht zu verwenden.

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> 
    <!-- Customize your theme here.> 
</style> 

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here.> 
</style> 
1

die Eltern Ihres Themas zu einer ohne Aktionsleiste ein. Zum Beispiel

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/mycolorprimary</item> 
    <item name="colorPrimaryDark">@color/mycolorprimarydark</item> 
    <item name="android:windowNoTitle">true</item> 
</style> 

Es wäre nicht notwendig, windowActionBar auf false zu setzen.