Android Studio getActionBar() 返回 null?

Posted

技术标签:

【中文标题】Android Studio getActionBar() 返回 null?【英文标题】:Android Studio getActionBar() returns null? 【发布时间】:2020-07-09 06:01:03 【问题描述】:

我在 Udemy 上学习了这个 5 年的 android 课程,并试图让这个应用程序在当前的 Android Studio 版本上运行,到目前为止我已经能够弄清楚如何至少让它构建,但是现在当我尝试在 Genymotion 上运行该应用程序,它第一次没有运行,当我第二次运行时,我得到了这个:

我在“logcat”选项卡中得到这个:

2020-07-08 22:41:34.665 2198-2198/? I/Zygote: seccomp disabled by setenforce 0
2020-07-08 22:41:34.666 2198-2198/? I/lta_bartalk_ne: Late-enabling -Xcheck:jni
2020-07-08 22:41:34.690 2198-2198/? W/lta_bartalk_ne: Unexpected CPU variant for X86 using defaults: x86
2020-07-08 22:41:34.814 2198-2223/com.example.delta_bartalk_new D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2020-07-08 22:41:34.809 2198-2198/com.example.delta_bartalk_new I/com.example.delta_bartalk_new: type=1400 audit(0.0:670): avc: denied  write  for comm=45474C20496E6974 name="property_service" dev="tmpfs" ino=10316 scontext=u:r:untrusted_app:s0:c70,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=1
2020-07-08 22:41:34.809 2198-2198/com.example.delta_bartalk_new I/com.example.delta_bartalk_new: type=1400 audit(0.0:671): avc: denied  connectto  for comm=45474C20496E6974 path="/dev/socket/property_service" scontext=u:r:untrusted_app:s0:c70,c256,c512,c768 tcontext=u:r:init:s0 tclass=unix_stream_socket permissive=1
2020-07-08 22:41:34.815 2198-2223/com.example.delta_bartalk_new D/vndksupport: Loading /vendor/lib/egl/libGLES_emulation.so from current namespace instead of sphal namespace.
2020-07-08 22:41:34.818 2198-2223/com.example.delta_bartalk_new E/libEGL: load_driver(/vendor/lib/egl/libGLES_emulation.so): dlopen failed: library "/vendor/lib/egl/libGLES_emulation.so" not found
2020-07-08 22:41:34.818 2198-2223/com.example.delta_bartalk_new D/vndksupport: Loading /vendor/lib/egl/libEGL_emulation.so from current namespace instead of sphal namespace.
2020-07-08 22:41:34.841 2198-2223/com.example.delta_bartalk_new D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2020-07-08 22:41:34.842 2198-2223/com.example.delta_bartalk_new D/vndksupport: Loading /vendor/lib/egl/libGLESv1_CM_emulation.so from current namespace instead of sphal namespace.
2020-07-08 22:41:34.856 2198-2223/com.example.delta_bartalk_new D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2020-07-08 22:41:34.866 2198-2198/com.example.delta_bartalk_new W/lta_bartalk_ne: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2020-07-08 22:41:34.889 2198-2223/com.example.delta_bartalk_new D/vndksupport: Loading /vendor/lib/egl/libGLESv2_emulation.so from current namespace instead of sphal namespace.
2020-07-08 22:41:34.891 2198-2223/com.example.delta_bartalk_new D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2020-07-08 22:41:34.894 2198-2198/com.example.delta_bartalk_new D/AndroidRuntime: Shutting down VM
    
    
    --------- beginning of crash
2020-07-08 22:41:34.895 2198-2198/com.example.delta_bartalk_new E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.delta_bartalk_new, PID: 2198
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.delta_bartalk_new/com.delta.bartalk_new.BartalkActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
        at com.delta.bartalk_new.BartalkActivity.onCreate(BartalkActivity.java:81)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

这是 BartalkActivity.java 文件:

package com.delta.bartalk_new;

import com.delta.bartalk_new.util.SystemUiHider;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 * @see SystemUiHider
 */

public class BartalkActivity extends Activity 
    /**
     * Whether or not the system UI should be auto-hidden after
     * @link #AUTO_HIDE_DELAY_MILLIS milliseconds.
     */
    private static final boolean AUTO_HIDE = true;

    /**
     * If @link #AUTO_HIDE is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

    /**
     * If set, will toggle the system UI visibility upon interaction. Otherwise,
     * will show the system UI visibility upon interaction.
     */
    private static final boolean TOGGLE_ON_CLICK = true;

    /**
     * The flags to pass to @link SystemUiHider#getInstance.
     */
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

    /**
     * The instance of the @link SystemUiHider for this activity.
     */
    private SystemUiHider mSystemUiHider;

    //
// Bartalk instance variables
//
    TextView mOutputText;
    EditText mInputText;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_bartalk);

//
// Bartalk code
//
        mOutputText = (TextView) findViewById(R.id.fullscreen_content);
        mInputText= (EditText) findViewById(R.id.input_text);

        mInputText.addTextChangedListener(new TextWatcher()
            public void afterTextChanged(Editable s) 
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            public void onTextChanged(CharSequence s, int start, int before, int count)
                mOutputText.setText(mInputText.getText());
            
        );

        ActionBar actionBar = getActionBar();
        actionBar.hide();

        final View controlsView = findViewById(R.id.fullscreen_content_controls);
        final View contentView = findViewById(R.id.fullscreen_content);

// Set up an instance of SystemUiHider to control the system UI for
// this activity.
        mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
        mSystemUiHider.setup();
        mSystemUiHider
                .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() 
                    // Cached values.
                    int mControlsHeight;
                    int mShortAnimTime;

                    @Override
                    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
                    public void onVisibilityChange(boolean visible) 
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) 
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
                            if (mControlsHeight == 0) 
                                mControlsHeight = controlsView.getHeight();
                            
                            if (mShortAnimTime == 0) 
                                mShortAnimTime = getResources().getInteger(
                                        android.R.integer.config_shortAnimTime);
                            
                            controlsView.animate()
                                    .translationY(visible ? 0 : mControlsHeight)
                                    .setDuration(mShortAnimTime);
                         else 
// If the ViewPropertyAnimator APIs aren’t
// available, simply show or hide the in-layout UI
// controls.
                            controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
                        

                        if (visible && AUTO_HIDE) 
// Schedule a hide().
                            delayedHide(AUTO_HIDE_DELAY_MILLIS);
                        
                    
                );

// Set up the user interaction to manually show or hide the system UI.
        contentView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 

                if (TOGGLE_ON_CLICK) 
                    mSystemUiHider.toggle();
                 else 
                    mSystemUiHider.show();
                
            
        );

// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
        findViewById(R.id.input_text).setOnTouchListener(mDelayHideTouchListener);
    

    @Override
    protected void onPostCreate(Bundle savedInstanceState) 
        super.onPostCreate(savedInstanceState);

// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
        delayedHide(100);
    


    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() 
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) 
            if (AUTO_HIDE) 
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            

            return false;
        
    ;

    Handler mHideHandler = new Handler();
    Runnable mHideRunnable = new Runnable() 
        @Override
        public void run() 
            mSystemUiHider.hide();
        
    ;

    /**
     * Schedules a call to hide() in [delay] milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) 
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    

我注意到,当我只是注释掉 getActionBar() 位时,它确实会运行,但我认为在使用该应用程序时某些内容不会“隐藏”。

我一直在研究整个“getActionBar() 返回 null”问题,我不断看到一堆答案,但似乎没有一个有效。使用getSupportActionBar() 的最有希望的方法甚至不起作用,因为当我尝试构建它时,Android Studio 抱怨它找不到该方法。

我需要做什么才能让这个getActionBar() 方法正常工作...?

更新 01 因此,按照Gabriele Mariotti's 的建议,我将导入语句更改为使用 AppCompatActivity(即使阅读了Jaiprakash Soni 链接的文档,我也不知道如何添加 v7 支持库)。这就是 BartalkActivity.java 文件现在的样子:

package com.delta.bartalk_new;

import com.delta.bartalk_new.util.SystemUiHider;

import android.annotation.TargetApi;
//import android.app.ActionBar;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
//import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 * @see SystemUiHider
 */

public class BartalkActivity extends AppCompatActivity 
    /**
     * Whether or not the system UI should be auto-hidden after
     * @link #AUTO_HIDE_DELAY_MILLIS milliseconds.
     */
    private static final boolean AUTO_HIDE = true;

    /**
     * If @link #AUTO_HIDE is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

    /**
     * If set, will toggle the system UI visibility upon interaction. Otherwise,
     * will show the system UI visibility upon interaction.
     */
    private static final boolean TOGGLE_ON_CLICK = true;

    /**
     * The flags to pass to @link SystemUiHider#getInstance.
     */
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

    /**
     * The instance of the @link SystemUiHider for this activity.
     */
    private SystemUiHider mSystemUiHider;

    //
// Bartalk instance variables
//
    TextView mOutputText;
    EditText mInputText;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_bartalk);

//
// Bartalk code
//
        mOutputText = (TextView) findViewById(R.id.fullscreen_content);
        mInputText= (EditText) findViewById(R.id.input_text);

        mInputText.addTextChangedListener(new TextWatcher()
            public void afterTextChanged(Editable s) 
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            public void onTextChanged(CharSequence s, int start, int before, int count)
                mOutputText.setText(mInputText.getText());
            
        );

        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        final View controlsView = findViewById(R.id.fullscreen_content_controls);
        final View contentView = findViewById(R.id.fullscreen_content);

// Set up an instance of SystemUiHider to control the system UI for
// this activity.
        mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
        mSystemUiHider.setup();
        mSystemUiHider
                .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() 
                    // Cached values.
                    int mControlsHeight;
                    int mShortAnimTime;

                    @Override
                    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
                    public void onVisibilityChange(boolean visible) 
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) 
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
                            if (mControlsHeight == 0) 
                                mControlsHeight = controlsView.getHeight();
                            
                            if (mShortAnimTime == 0) 
                                mShortAnimTime = getResources().getInteger(
                                        android.R.integer.config_shortAnimTime);
                            
                            controlsView.animate()
                                    .translationY(visible ? 0 : mControlsHeight)
                                    .setDuration(mShortAnimTime);
                         else 
// If the ViewPropertyAnimator APIs aren’t
// available, simply show or hide the in-layout UI
// controls.
                            controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
                        

                        if (visible && AUTO_HIDE) 
// Schedule a hide().
                            delayedHide(AUTO_HIDE_DELAY_MILLIS);
                        
                    
                );

// Set up the user interaction to manually show or hide the system UI.
        contentView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 

                if (TOGGLE_ON_CLICK) 
                    mSystemUiHider.toggle();
                 else 
                    mSystemUiHider.show();
                
            
        );

// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
        findViewById(R.id.input_text).setOnTouchListener(mDelayHideTouchListener);
    

    @Override
    protected void onPostCreate(Bundle savedInstanceState) 
        super.onPostCreate(savedInstanceState);

// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
        delayedHide(100);
    


    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() 
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) 
            if (AUTO_HIDE) 
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            

            return false;
        
    ;

    Handler mHideHandler = new Handler();
    Runnable mHideRunnable = new Runnable() 
        @Override
        public void run() 
            mSystemUiHider.hide();
        
    ;

    /**
     * Schedules a call to hide() in [delay] milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) 
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    

但现在它给了我同样的“继续停止”错误,但在 logcat 中得到了不同的错误:

2020-07-09 10:35:50.719 2135-2135/? I/Zygote: seccomp disabled by setenforce 0
2020-07-09 10:35:50.721 2135-2135/? I/lta_bartalk_ne: Late-enabling -Xcheck:jni
2020-07-09 10:35:50.751 2135-2135/? W/lta_bartalk_ne: Unexpected CPU variant for X86 using defaults: x86
2020-07-09 10:35:50.912 2135-2160/com.example.delta_bartalk_new D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2020-07-09 10:35:50.909 2135-2135/com.example.delta_bartalk_new I/com.example.delta_bartalk_new: type=1400 audit(0.0:718): avc: denied  write  for comm=45474C20496E6974 name="property_service" dev="tmpfs" ino=9292 scontext=u:r:untrusted_app:s0:c70,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=1
2020-07-09 10:35:50.915 2135-2160/com.example.delta_bartalk_new D/vndksupport: Loading /vendor/lib/egl/libGLES_emulation.so from current namespace instead of sphal namespace.
2020-07-09 10:35:50.921 2135-2160/com.example.delta_bartalk_new E/libEGL: load_driver(/vendor/lib/egl/libGLES_emulation.so): dlopen failed: library "/vendor/lib/egl/libGLES_emulation.so" not found
2020-07-09 10:35:50.921 2135-2160/com.example.delta_bartalk_new D/vndksupport: Loading /vendor/lib/egl/libEGL_emulation.so from current namespace instead of sphal namespace.
2020-07-09 10:35:50.943 2135-2160/com.example.delta_bartalk_new D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2020-07-09 10:35:50.944 2135-2160/com.example.delta_bartalk_new D/vndksupport: Loading /vendor/lib/egl/libGLESv1_CM_emulation.so from current namespace instead of sphal namespace.
2020-07-09 10:35:50.950 2135-2160/com.example.delta_bartalk_new D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2020-07-09 10:35:50.982 2135-2135/com.example.delta_bartalk_new D/AndroidRuntime: Shutting down VM
2020-07-09 10:35:50.985 2135-2135/com.example.delta_bartalk_new E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.delta_bartalk_new, PID: 2135
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.delta_bartalk_new/com.delta.bartalk_new.BartalkActivity: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:552)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.delta.bartalk_new.BartalkActivity.onCreate(BartalkActivity.java:66)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2020-07-09 10:35:50.992 2135-2160/com.example.delta_bartalk_new D/vndksupport: Loading /vendor/lib/egl/libGLESv2_emulation.so from current namespace instead of sphal namespace.
2020-07-09 10:35:50.994 2135-2160/com.example.delta_bartalk_new D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so

所以现在它在抱怨setContentView(R.layout.activity_bartalk);。这是为什么?

【问题讨论】:

Im following this 5 year old Android course on Udemy - 不要那样做。 Android 在过去 5 年中发生了一些变化。您可以开始更改AppCompatActivity 中的Activity,然后您可以使用getSupportActionBar() 关注此文档developer.android.com/training/appbar/setting-up#java 【参考方案1】:

在更仔细地检查了 logcat 输出后,我注意到一些关于 AppCompat 主题的内容,进一步查找,发现了这张票: How to fix: "You need to use a Theme.AppCompat theme (or descendant) with this activity"

按照此操作,并在 values/colors.xml 中添加我自己的文本颜色后,我能够在 Genymotion 中正确加载。感谢Gabriele Mariotti 为我提供使用AppCompatActivity 的提示。

【讨论】:

以上是关于Android Studio getActionBar() 返回 null?的主要内容,如果未能解决你的问题,请参考以下文章

android studio setting 在哪

如何解决android studio2.3导入android studio2.1的问题

Android studio上找不到setting?

android studio v4包冲突

如何用android studio 编成app

如何手动升级android studio?