gpt4 book ai didi

android - getActionBar() 在 api 19 中返回 null,目标是 api 21 min sdk 16

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:37 25 4
gpt4 key购买 nike

我的应用程序在运行 kitkat(api 19) 的设备上崩溃,我在 LG g3 和 nexus 7 2013 上试过,因为 nullPointer 预期。

我发现当我执行 getActionBar() 时它返回 null 并且我无法修复它。

我为运行 lollipop 的设备使用 appcompat 主题 21 和 material 主题,应用程序在这些设备上运行良好。

主要 Activity .java:

package com.sqvat.betterpowermat;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends Activity {

private ActionBar actionBar;

private String[] categories;
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;

private String drawerClose;
private String drawerOpen;

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

actionBar = getActionBar();

//Nav Drawer
categories = getResources().getStringArray(R.array.categories);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);

drawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.nav_drawer_li, R.id.drawer_li_textview, categories));

drawerList.setOnItemClickListener(new DrawerItemClickListener());

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.drawerOpen, R.string.drawerClose) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};

// Set the drawer toggle as the DrawerListener
drawerLayout.setDrawerListener(drawerToggle);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);

selectItem(0);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}

private void selectItem(int position) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (position){
case 0:
fragmentTransaction.replace(R.id.content_frame, new HomeFragment());
actionBar.setTitle("Home");
break;
case 1:
fragmentTransaction.replace(R.id.content_frame, new LocationsFragment());
actionBar.setTitle("Locations");
break;
case 2:
fragmentTransaction.replace(R.id.content_frame, new StoreFragment());
actionBar.setTitle("Store");
break;
case 3:
fragmentTransaction.replace(R.id.content_frame, new MoreFragment());
actionBar.setTitle("More");
break;




}
fragmentTransaction.commit();

drawerList.setItemChecked(position, true);
drawerLayout.closeDrawer(drawerList);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...

return super.onOptionsItemSelected(item);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

我的 appcompat 样式 (values/styles.xml):

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

</style>

<color name="colorPrimary">#4CAF50</color>
<color name="colorPrimaryDark">#43A047</color>

我的 android Lollipop 样式 (values-v21/styles.xml):

<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

最佳答案

如果你想在支持库上使用ActionBar,你必须扩展ActionBarActivity类而不是Activity,并使用getSupportActionBar () 代替 getActionBar() 来获取 ActionBar

关于android - getActionBar() 在 api 19 中返回 null,目标是 api 21 min sdk 16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26871775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com