空对象引用上的'void android.support.v7.app.ActionBar.setDefaultDisplayHomeAsUpEnabled(boolean)'

时间:2016-02-04 06:18:41

标签: android

请参阅我的以下代码。我已在styles.xml更新了所有最新代码。扩展了AppCompatActivity并使用了支持库。请指导我,提前谢谢。

package com.example.preciset3.tuesday;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity
{
 ListView listView;
    ActionBar actionBar;
     ActionBarDrawerToggle actionBarDrawerToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
//        actionBar.setDisplayHomeAsUpEnabled(true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView= (ListView) findViewById(R.id.navbar);
        android.widget.ArrayAdapter arrayAdapter= ArrayAdapter.createFromResource(this, R.array.Items, android.R.layout.simple_list_item_1);
        listView.setAdapter(arrayAdapter);
     ActionBar  actionBar =getSupportActionBar();
//        (this).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      //  actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    actionBar.setDefaultDisplayHomeAsUpEnabled(true);
    //    actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,)

    }

}

3 个答案:

答案 0 :(得分:0)

您正在使用AppCompatActivitygetSupportActionBar(),所以我想您检查一下styles.xml并查看您的父母AppCompat主题。

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">true</item>
    ...
</style>

答案 1 :(得分:0)

请按照以下步骤添加工具栏,如评论中提到的步骤。首先改变风格

<强> Styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="homeAsUpIndicator">@drawable/camera</item>

    </style>

然后创建工具栏布局。

<强> toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/gradient_toolbar"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light">

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

然后在您的Activity布局

<强> acitivyt_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar" />
    </RelativeLayout>
</ScrollView>

然后在MainActivity

<强> MainActivity

public class MainActivity extends AppCompatActivity{
    private ImageView iv_profilePic, ivEditImage;
    private EditText etName, etEmail, etPhoneNumber;
    private ImageChooserManager imageChooserManager;
    private String filePath = "";
    private int chooserType;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

希望这有帮助。

答案 2 :(得分:0)

检查

package com.example.preciset3.tuesday;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity
{
    ListView listView;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView= (ListView) findViewById(R.id.navbar);
        android.widget.ArrayAdapter arrayAdapter= ArrayAdapter.createFromResource(this, R.array.Items, android.R.layout.simple_list_item_1);
        listView.setAdapter(arrayAdapter);

       assert getSupportActionBar() != null;
       getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    }

}