Reputation: 2790
On android device 4.0.1 I am trying to build application with ActionBar
but getting the NullPointException
. I have tried the following solutions:
Theme.Holo.Light
to the application theme.OnCreate
of Activity, setting ActionBar
feature before setContentView
as requestWindowFeature(Window.FEATURE_ACTION_BAR)
.SherlockActivity
and called getSupportActionBar()
But no luck. In all methods, I am getting null ActionBar
. Can anyone please point me out what is the issue. I am pasting Activity
and AndroidManifest.xml
here.
<code>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tzoomers.birthdaysdiary"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light">
<activity
android:name="com.tzoomers.birthdaysdiary.BirthdaysDiary"
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=".ContentActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SyncActivity">
</activity>
</application>
</manifest>
</code>
<code>
public class SyncActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.layout_sync_activity);
ActionBar actionBar = getActionBar();
if(actionBar != null)
{
getActionBar().setDisplayHomeAsUpEnabled(false);
}
else
{
Toast.makeText(this, "Action bar is null", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
}
Please help what can be exact issue instead of pointing urls
. I have tried all the solutions. If I am missing something in XML
or JAVA
files please point that.
Thanks in advance.
Upvotes: 2
Views: 588
Reputation: 11
I JUST solved this problem myself, with the new updates its a bit tricky and some of the old fixes don't work as well anymore. Try this:
set your MainActivity
Java code to extend ActionBarActivity
use getSupportAcionBar();
call to retrieve your action bar
Be sure your (custom) TabListener extends android.support.v7.app.ActionBar.TabListener
you can use FragmentManager to add and remove tab Fragments when tabs are selected and unselected.
Here's some of my code snippets to help illustrate, hope it works for you too :)
public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) //Overrode Default Constructor
{
super.onCreate(savedInstanceState);
android.support.v7.app.ActionBar tabsActionBar = getSupportActionBar();
/***following changes ActionBar to a Tabbed ActionBar***/
tabsActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);`
android.support.v7.app.ActionBar.Tab tabArray = tabsActionBar.newTab();to later contain 'inflated' digitalClock (tabbed) UI
tabArray.setText(R.string.tab_one);
/***following sets clockTabListener private class as Listener for
* this object***/
tabArray.setTabListener( new clockTabListener( this, digitalClockFragment.class.getName() ) );//actual call to digitalClockFragment
tabsActionBar.addTab(tabArray);//adds TabArray to Action Bar Tab(s)
/******Second call for setting New Tab to AnalogClockFragment******/
tabArray = tabsActionBar.newTab();
tabArray.setText(R.string.tab_two);
tabArray.setTabListener( new clockTabListener( this, analogClockFragment.class.getName() ) );//actual call to analogClockFragment
tabsActionBar.addTab(tabArray);
private class clockTabListener implements android.support.v7.app.ActionBar.TabListener
{
private final Activity currentActivity;
private final String currentFragment;
private Fragment launchFragment;
private android.app.FragmentManager frgManager;
public clockTabListener(Activity activityName, String fragmentName)
{
currentActivity = activityName;
currentFragment = fragmentName;
frgManager = getFragmentManager();
}
/******************************************************************/
/******************************************************************/
/******************************************************************/
@Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1)
{
launchFragment = Fragment.instantiate(currentActivity, currentFragment);
frgManager.beginTransaction().replace(android.R.id.content, launchFragment).commit();
}
Upvotes: 1