草庐IT

Android ActionBar 始终为空

coder 2023-12-01 原文

我有一个带有抽屉导航的 Activity ,它在 fragment 之间轻弹。没什么特别的。

但是我想要的是操作栏出现在 Activity 的顶部。问题是我不断收到 Actionbarnullpointerexception

我试过以下页面的答案:

getActionBar returns null

getActionBar() returns Null (AppCompat-v7 21)

这些都没有用,所以我想我会用我自己的代码提交我自己的问题。

主 Activity .java

package com.jampez.smalltalk;


import com.jampez.smalltalk.fragments.HomeFragment;
import com.jampez.smalltalk.fragments.MessageFragment;
import com.jampez.smalltalk.fragments.PreferencesFragment;
import com.jampez.smalltalk.fragments.SettingsFragment;
import com.jampez.smalltalk.helper.SessionManager;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends FragmentActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private String[] items;
    int selectedPosition;
    private SessionManager session;

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

    setContentView(R.layout.activity_main);
    getActionBar().setTitle("");

    // Session manager
    session = new SessionManager(getApplicationContext());

    if (!session.isLoggedIn()) {
        // User is already logged in. Take him to main activity
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    items = getResources().getStringArray(R.array.menus);
    // Getting reference to the DrawerLayout 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.drawer_list);

    /* Creating an ArrayAdapter to add items to mDrawerList */
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, items);
    mDrawerList.setAdapter(adapter);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, 
            R.string.drawer_open) {

        /* Called when drawer is closed */
        public void onDrawerClosed(View view) {
            //Put your code here
        }

        /* Called when a drawer is opened */
        public void onDrawerOpened(View drawerView) {
            //Put your code here
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);


    // Enabling Home button
    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    // Setting item click listener for the listview mDrawerList
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectedPosition = position;

            /* Replace fragment content */
            displayView(position);

           /* Closing the drawer */
            mDrawerLayout.closeDrawer(mDrawerList);
        }
    });

    /* Setting default fragment */
    selectedPosition = 0;
    displayView(0);
}

private void displayView(int position) {
    // update the main content by replacing fragments
    android.app.Fragment fragment = null;
    Log.i("position", position+"");
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new MessageFragment();
        break;
    case 2:
        fragment = new PreferencesFragment();
        break;
    case 3:
        fragment = new SettingsFragment();
        break;
    case 4:
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
        sendIntent.setType("text/plain");
        startActivity(sendIntent);
        break;
    case 5:
        String url = "http://www.example.com";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
        break;
        default:
            break;
    }

    if (fragment != null) {

        android.app.FragmentManager fragmentManager = getFragmentManager();
        android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, fragment);
        fragmentTransaction.commit();


        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);

        //setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}
}

activity_main.xml

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@android:style/Theme.WithActionBar" >

<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- The navigation drawer list-->
<ListView
    android:id="@+id/drawer_list"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

首页 fragment .java

package com.jampez.smalltalk.fragments;

import com.jampez.smalltalk.R;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment {

public HomeFragment(){}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    //getActivity().getActionBar().setTitle(R.string.home);

    return rootView;
}
}

fragment _home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:orientation="vertical" >

<TextView
    android:id="@+id/home_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:layout_gravity="start|center_vertical"
    android:text="@string/home"/>    
</LinearLayout>

Android list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jampez.smalltalk"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="23" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:name=".app.AppController"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize|stateHidden" >
    </activity>
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
</application>

样式.xml

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->

</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
     <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@color/white</item>
</style>

<style name="MyActionButtonOverflow"     parent="android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:color">@color/white</item>
</style>

 <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#25a0da</item>
</style>

Logcat 输出

11-08 19:17:38.618: E/AndroidRuntime(6347): FATAL EXCEPTION: main
11-08 19:17:38.618: E/AndroidRuntime(6347): Process: com.jampez.smalltalk, PID: 6347
11-08 19:17:38.618: E/AndroidRuntime(6347): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jampez.smalltalk/com.jampez.smalltalk.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
11-08 19:17:38.618: E/AndroidRuntime(6347):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at android.app.ActivityThread.access$900(ActivityThread.java:172)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at android.os.Looper.loop(Looper.java:145)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at android.app.ActivityThread.main(ActivityThread.java:5832)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at java.lang.reflect.Method.invoke(Native Method)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at java.lang.reflect.Method.invoke(Method.java:372)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
11-08 19:17:38.618: E/AndroidRuntime(6347): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
11-08 19:17:38.618: E/AndroidRuntime(6347):     at com.jampez.smalltalk.MainActivity.onCreate(MainActivity.java:37)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at android.app.Activity.performCreate(Activity.java:6221)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
11-08 19:17:38.618: E/AndroidRuntime(6347):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
11-08 19:17:38.618: E/AndroidRuntime(6347):     ... 10 more

最佳答案

*首先在你的gradle文件中包含这个依赖

compile 'com.android.support:appcompat-v7:22.2.0'

*您应该将 Activity 扩展到 AppCompactActivity 并开始使用 toolbar 而不是 ActionBar

*这就是您在 MainActivity

中使用工具栏的方式
public class MainActivity extends AppCompatActivity {

    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
}

*这是在 activity_main.xml 中添加工具栏的方法

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

*你的主题应该看起来像这样 styles.xml

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

确保在您的应用程序的 list 中添加此样式名称。

*如果你使用v4.app.Fragment而不是使用Fragment会更好。

*这是你应该如何从你的 fragment 类设置标题

((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(R.string.home);

引用this清晰的教程,它一定会帮助您设置 Material 抽屉导航。

如果您想快速开始而不会有太多痛苦,那么您可以尝试整合 this第三方库。

关于Android ActionBar 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597986/

有关Android ActionBar 始终为空的更多相关文章

  1. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  2. ruby - 从谷歌开发者网站下载后,client_secret.json 为空 - 2

    我正在尝试从googleAPI下载client_secret.json。我正在执行https://developers.google.com/gmail/api/quickstart/ruby中列出的步骤.使用此向导在GoogleDevelopersConsole中创建或选择项目并自动启用API。在左侧边栏中,选择同意屏幕。选择电子邮件地址并输入产品名称(如果尚未设置),然后单击“保存”按钮。在左侧边栏中,选择凭据并点击创建新客户端ID。选择应用程序类型已安装应用程序,已安装应用程序类型为其他,然后单击“创建客户端ID”按钮。点击新客户端ID下的下载JSON按钮。将此文件移动到您的工作

  3. ruby - 有没有更简洁的方法来检查 PGResult 是否为空? - 2

    我正在使用pggem从Ruby与PostgreSQL对话。有没有检查是否没有结果比使用res.ntuples==0更好的方法?conn=PGconn.connectconfigcmd="select*fromlabelsinnerjoinlabels_mailusing(label_id)"+"wherelabels_mail.mail_id=$1andlabels.name=$2"res=conn.exec(cmd,[mail_id,mailbox])ifres.ntuples==0# 最佳答案 元组,打字错误?使用zero?比=

  4. ruby - @users 变量为空。忘记为 will_paginate 传递集合对象? - 2

    问题localhost:3000/users/不会显示我谦虚地进入,因为我是第一次尝试通过Rails教程。我在第10章,我已经花了5个小时解决这个问题。当我尝试访问localhost:3000/users/时出现错误(我相信这与factory_girl有关)解释了@users变量为空并且我忘记了为will_paginate传递一个集合对象。我目前在第10章第10.23节,每次运行时:$bundleexecrakedb:reset$bundleexecrakedb:populate$bundleexecrakedb:test:prepare我在解释时遇到错误rakeaborted!Fac

  5. ruby-on-rails - 参数缺失或值为空 - 2

    我是Rails新手。得到下面的错误。我了解问题所在,但不确定如何解决?错误-参数缺失或值为空:客户defcustomer_paramsparams.require(:customer).permit(:first_name,:last_name,:email,:password,:password_confirmation)endendDetailsController.rbclassMy::Account::DetailsController:showelseredirect_tomy_account_details_path,:notice=>'Accountdetailsupda

  6. ruby-on-rails - 如果参数为空,则 Text_field_tag 默认值? - 2

    如果params[:date]的参数为空,我希望我的text_field_tag将当前日期作为默认值,这是我目前的代码:我想要类似:谢谢 最佳答案 您可以简单地使用“或”运算符。如果params[:end]为空,它将使用Time.now。 关于ruby-on-rails-如果参数为空,则Text_field_tag默认值?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/35520

  7. Ruby 的 ARGV 在 Windows 上可以为空,具体取决于运行脚本的方式 - 2

    我的演示.rb:putsARGV.sizeARGV.eachdo|a|puts"Argument:#{a}"end结果取决于我们如何运行脚本:>demo.rbfoobar0>rubydemo.rbfoobar2Argument:fooArgument:bar为什么会这样?可以用这个做点什么吗?编辑:感谢所有回复!这是我的设置:>assoc.rb.rb=rbFile>ftyperbFilerbFile="c:\ruby-1.8.6\bin\ruby.exe""%1"%*所以看起来是对的。但是我发现了>demo.rbfoobar使用这样的命令行启动进程:"C:\ruby-1.8.7\bin

  8. ruby - 如果 `self` 始终是 Ruby 中的隐含接收者,为什么 `self.puts` 不起作用? - 2

    在Ruby中,我的理解是self是任何裸方法调用的隐含接收者。然而:~:irb>>puts"foo"foo=>nil>>self.puts"foo"NoMethodError:privatemethod`puts'calledformain:Object这是什么原因?如果有任何帮助:>>method(:puts).owner=>Kernel 最佳答案 私有(private)方法不能有接收者我认为答案是这样的:Ruby强制方法隐私的方式是它不允许使用显式接收者调用私有(private)方法。一个例子:classBakerdefbake

  9. ruby-on-rails - 即使在编辑模式下,rails 嵌套形式始终为 "POST"?导致路由错误 - 2

    我正在开发一个应用程序有1个具有多对多关系的模块,它工作正常,我可以用嵌套形式创建我的“单元”,但是当我处于编辑状态时我遇到了1个问题,当我点击提交按钮时它抛出了一个错误。Noroutematches[POST]"/units/27"ithoughttheactionshouldbepatchinsteadofpost??iinspectthegeneratedhtmlinbrowsers,andicanseethereisahiddenfieldnamed"_method"with"patch"value.我用脚手架生成了这个模块单位模型classUnit:unitrentperio

  10. ruby - Jekyll:检查帖子内容是否为空 - 2

    我在Jekyll项目中有一系列帖子,其中一些只有标题,一些有标题和内容。我想在每种情况下对帖子做不同的事情。例如:{%forpostinsite.categories.publications%}{{post.title}}{%ifpost.content==""orpost.content==nilorpost.content==blank%}Nothinghere.{%else%}{{post.content}}{%endif%}{%endfor%}但是if语句实际上并没有捕获空帖子。我的条件基于thispage,但是这3种可能性都没有捕捉到空帖子。关于如何处理这些帖子有什么想法吗

随机推荐