我在运行时收到此错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setNavigationMode(int)' on a null object reference
主要Activity.java
public class MainActivity <T extends Fragment> extends ActionBarActivity implements ListView.OnItemClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
ActionBar.Tab tab;
tab = actionBar.newTab()
.setText("Categories")
.setTabListener(new TabListener<CategoriesFragment>(
this, "Categories", CategoriesFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Acceuil")
.setTabListener(new TabListener<AcceuilFragment>(
this, "Acceuil", AcceuilFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Topics")
.setTabListener(new TabListener<TopicsFragment>(
this, "Topics", TopicsFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Tendances")
.setTabListener(new TabListener<TendancesFragment>(
this, "Tendances", TendancesFragment.class));
actionBar.addTab(tab);
setRef();
//Set the custom toolbar
if (toolbar != null)
{
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
}
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.open,
R.string.close
)
{
public void onDrawerClosed(View view)
{
Log.d("MainActivity", "OnDrawerClosed");
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
Log.d("MainActivity", "OnDrawerOpened");
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
// specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
private void setRef()
{
// create reference to the xml views
mRecyclerView = (android.support.v7.widget.RecyclerView) findViewById(R.id.my_recycler_view);
fab = (Button) findViewById(R.id.addButton);
drawerLayout = (android.support.v4.widget.DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setStatusBarBackground(R.color.primarydark);
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
leftDrawerList = (ListView) findViewById(R.id.list_drawer);
View list_header = getLayoutInflater().inflate(R.layout.drawerlist_header, null);
leftDrawerList.addHeaderView(list_header);
navigationDrawerAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, leftSliderData);
leftDrawerList.setAdapter(navigationDrawerAdapter);
leftDrawerList.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(getApplicationContext(), "Clicked on " + leftSliderData[position - 1], Toast.LENGTH_LONG).show();
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- Setting this one to true makes the status bar
to have a non-transparent shitty background -->
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<include layout="@layout/actionbarlayout"></include>
<include layout="@layout/content_view"></include>
<Button
android:id="@+id/addButton"
android:text="+"
android:textSize="25sp"
android:textColor="@android:color/white"
android:textAlignment="center"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:background="@drawable/circlebtn"
android:layout_width="56dp"
android:layout_height="56dp"
android:stateListAnimator="@animator/anim"
android:elevation="4dp"
style="?android:buttonStyleSmall"
android:layout_gravity="right|bottom" >
</Button>
</FrameLayout>
<com.androprogrammer.test.materialapp1.ScrimInsetsFrameLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:elevation="8dp"
android:layout_gravity="start"
app:insetForeground="#4000">
<ListView
android:id="@+id/list_drawer"
android:layout_width="@dimen/drawer_width"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:background="@android:color/white"
android:fitsSystemWindows="true">
</ListView>
</com.androprogrammer.test.materialapp1.ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
值/ styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primarydark</item>
<item name="colorAccent">@color/primary</item>
<item name="colorControlHighlight">@color/primary</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
</resources>
V21 / styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androprogrammer.test.materialapp1" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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>
</application>
</manifest>
请帮助:)
答案 0 :(得分:3)
尝试更改父样式
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
不推荐使用和ActionBarActivity
,您应该使用AppCompatActivity
答案 1 :(得分:0)
在style.xml中,您必须启用操作栏。如果操作栏被禁用(设置为false),那么它将不会在运行时添加到活动中(即getActionBar()
返回null
)。