Tab Fragment Tutorial failing on build - android

I am working with this tutorial to teach myself tab fragments. When I paste and run the MainActivity I get this error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
at hss.fragmenttabstutorial.MainActivity.onCreate(MainActivity.java:27)
So I changed the Activity to ActionBarActivity, and changed the ActionBar to getSupportActionBar like many suggested. Now it won't build due to getSupportActionBar, stating "Incompatible types". What should I do?
Here's the Main code:
import android.app.Activity;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Fragment;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
// Declaring our tabs and the corresponding fragments.
ActionBar.Tab bmwTab, fordTab, toyotaTab;
Fragment bmwFragmentTab = new FragmentTab1();
Fragment toyotaFragmentTab = new FragmentTab2();
Fragment fordFragmentTab = new FragmentTab3();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Asking for the default ActionBar element that our platform supports.
ActionBar actionBar = getSupportActionBar();
// Screen handling while hiding ActionBar icon.
actionBar.setDisplayShowHomeEnabled(false);
// Screen handling while hiding Actionbar title.
actionBar.setDisplayShowTitleEnabled(false);
// Creating ActionBar tabs.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Setting custom tab icons.
bmwTab = actionBar.newTab().setText("Fragment1");
toyotaTab = actionBar.newTab().setText("Fragment2");
fordTab = actionBar.newTab().setText("Fragment3");
// Setting tab listeners.
bmwTab.setTabListener(new TabListener(bmwFragmentTab));
toyotaTab.setTabListener(new TabListener(toyotaFragmentTab));
fordTab.setTabListener(new TabListener(fordFragmentTab));
// Adding tabs to the ActionBar.
actionBar.addTab(bmwTab);
actionBar.addTab(toyotaTab);
actionBar.addTab(fordTab);
}
}

Instead of import android.app.ActionBar use android.support.v7.app.ActionBar.
This ensures compatibility with the rest of the support library including ActionBarActivity.

Related

ActionBar didn't work?

My application crashes and I get
Error :
setNavigationMode(int)' on a null object reference
Code :
package com.example.muhammad_adel.tabs;
import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("one"));
actionBar.addTab(actionBar.newTab().setText("two"));
actionBar.addTab(actionBar.newTab().setText("three"));
}
}
Change 1 : instead of import android.support.v4.app.ActionBar use import android.support.v7.app.ActionBar in your activity
Change 2 : change this actionBar = getActionBar(); to actionbar = getSupportActionBar();
Update:
You have to use PagerSlidingTabStrip because setNavigationMode is now deprecated.
It's been long time since ActionBar got deprecated.
Try to use Toolbar and SlidingTabs.
Please refer to this answer for same question. It will provide you enough guide for your query.
Android, Tabs without Actionbar
You can easily implement this and it will be easy for you to handle onClick() events for tabs.
I find the solution thanks for your help
First instead of import android.app.ActionBar => import android.support.v7.app.ActionBar
Second changing this actionBar = getActionBar(); to actionbar = getSupportActionBar();
Third you must implement TabListener
You should Try to change your parent style
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

How to remove ActionBarActivity?

I am successfully running the Activity, but I see the titlebar above the scrolltabs. I want the titlebar to be removed, when I try:
public class Iphone6Activity extends Activity
I get error Cannot resolve method 'getSupportFragmentManager()'
Below is my main Activity:
package com.hashmi.omar.vodafonestore;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class Iphone6Activity extends ActionBarActivity {
// Declaring Your View and Variables
ViewPager pager;
ViewPagerAdapter_ip6 adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Overview","Specifications", "Choose a Bundle"};
int Numboftabs =3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iphone6);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles for the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter_ip6(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.ip6pager);
pager.setAdapter(adapter);
// Assigning the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.ip6tabs);
// Setting the ViewPager for the SlidingTabsLayout
tabs.setViewPager(pager);
}
}
How can I remove actionbaractivity?
ActionBarActivity is deprecated, so I suggest first change it to AppCompatActivity:
public class Iphone6Activity extends AppCompatActivity
and in your styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
inside your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.getActionBar().setTitle("");
return super.onCreateOptionsMenu(menu);
}
in your manifest:
<activity
android:name="your activity"
android:configChanges="orientation|screenSize"
android:label="Label of your Activity HERE"
android:launchMode="singleTop">
...
choose one of them, in your manifest just set android:label to a empty String (if you choose manifest way)
Take a look at this question: Cannot call getSupportFragmentManager() from activity
Also, to remove title bar from your view, change the theme of your layout xml or your android manifest(this would change the theme of the entire application, you might have to use a toolbar if you want custom toolbars in activities) to something like Theme.AppCompat.Light.NoActionBar.
Lastly, ActionBarActivity extends Activity so you don't really need to change it.

Can get the actionBar reference

This is the code for my activity.
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBar;
public class MainActivity extends FragmentActivity {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
}
}
The problem is that the getActionBar() is red lined (Type mismatch: cannot convert from android.app.ActionBar to android.support.v7.app.ActionBar) but if you see the imports it is intended to be of support library.
Well I just want to have a an actionbar reference as I am working with swiping tabs and which belong to support library.
I mean can I get a reference to the actionBar for support library as I can have for non-support i.e. ActionBar ab = getActionBar(); ?

Start activity without ActionBar and add it in the onCreate event

I'm using the AppCompat/ActionBarCompat library and I need to create a custom ActionBar. I need to open the activity without an ActionBar and enable it only when I add the custom view. How can I do this?
PS: I need to define the activity to not use an ActionBar through the AndroidManifest.xml and my application minimum API level is 10.
.hide() the action bar and then .show() the action bar when your ready for it
import android.support.v7.app.ActionBar;
...
private ActionBar mActionbar;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionbar = getSupportActionBar();
mActionbar.hide()
}
...
somewhere when something cool happens
mActionbar.show();

PreferenceFragment can't load from XML (ClassCastException)

I'm having some difficulties getting a PreferenceFragment to load using XML.
It keeps throwing a ClassCastException "PreferenceFragmentClass cannot be cast to android.support.v4.app.Fragment".
The code is intended to run on API14 and higher.
Here's my code:
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Here's the code where SettingsFragment is being used in:
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class FragmentsSetup extends FragmentActivity {
private ViewPager viewPager;
private TabsAdapter tabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewPager = new ViewPager(this);
viewPager.setId(R.id.pager);
setContentView(viewPager);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tabsAdapter = new TabsAdapter(this, viewPager);
tabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.forwarding_tab).toUpperCase()), ForwardingFragment.class, null);
tabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.settings_tab).toUpperCase()), SettingsFragment.class, null);
}
}
Any tips would be greatly appreciated.
SettingsFragment inherits from PreferenceFragment. PreferenceFragment is from the native API Level 11 implementation of fragments. However, FragmentsSetup is inheriting from FragmentActivity, from the Android Support package's backport of fragments.
That combination will not work.
If you intend to support devices older than API Level 11, you cannot use PreferenceFragment. Also, I am uncertain if PreferenceFragment works outside of a PreferenceActivity (it might, but I have never tried that).
If you're developing for Android 3.0 (API level 11) and higher, you should use a PreferenceFragment to display your list of Preference objects.
Also you can add a PreferenceFragment to any activity—you don't need to use PreferenceActivity.
See documentation here:
https://developer.android.com/guide/topics/ui/settings.html?

Categories

Resources