必需:android.support.v7.app.ActionBar找到:void

时间:2018-04-12 10:36:36

标签: android android-fragments

我正在尝试这样做:

ActionBar toolbar = getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);

但它说: 必需:android.support.v7.app.ActionBar找到:void。 不知道该怎么办,希望有人可以提供帮助。感谢

它也说: 只能在同一个库组(groupId = com.android.support)中调用ActionBar.setDefaultDisplayHomeAsUpEnabled少...(Ctrl + F1) 此API已标记为尚未满足的限制。 API限制的示例:*方法只能由子类调用*方法只能在同一个库中访问(由Gradle库组ID定义)。*方法只能从测试中访问。 。您可以使用@RestrictTo注释添加自己的API限制。

之后我使用了这个:

ActionBar toolbar = getSupportActionBar();
toolbar.setDisplayHomeAsUpEnabled(true);

我现在正在接受这个例子:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.hfad.appmovienav, PID: 13025
                  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hfad.appmovienav/com.hfad.appmovienav.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2718)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                      at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
                      at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
                      at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:56)
                      at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
                      at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
                      at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
                      at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
                      at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                      at android.support.v7.app.AppCompatActivity.getSupportActionBar(AppCompatActivity.java:109)
                      at com.hfad.appmovienav.MainActivity.<init>(MainActivity.java:24)
                      at java.lang.Class.newInstance(Native Method)
                      at android.app.Instrumentation.newActivity(Instrumentation.java:1173)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6541) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Application terminated.

1 个答案:

答案 0 :(得分:0)

ActionBar toolbar = getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);

在上面的行中,您从setDefaultDisplayHomeAsUpEnabled(true)获取返回void值。所以你需要这样做:

ActionBar toolbar = getSupportActionBar();
toolbar.setDisplayHomeAsUpEnabled(true);

不要忘记将您的活动扩展到AppCompatActivity 喜欢:

public class BaseActivity extends AppCompatActivity {

并添加样式:

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

并在清单中使用此样式:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">