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?
Related
I know this question has been posted plenty of times, but I still unable to solve even i have tried many solution from Stack Overflow.
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import com.example.project.myapplication.API.InfoAPI;
import com.example.project.myapplication.Adapter.TabsFragmentPagerAdapter;
public class Edit extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private ActionBar actionBar;
private TabsFragmentPagerAdapter tabsAdapter;
private String[] activities = new String[]{"Information","Work Force","Work Details"};
ListView listViewEdit;
InfoAPI sqlcon;
private MyDatabaseHelper dbHelper;
private SQLiteDatabase database;
private SimpleCursorAdapter dataAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
viewPager = (ViewPager) findViewById(R.id.viewPager);
tabsAdapter = new TabsFragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabsAdapter);
actionBar = (ActionBarActivity)getApplicationContext().getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(int i=0; i<3; i++){
actionBar.addTab(actionBar.newTab().setText(activities[i]).setTabListener(this));
}
Error is on this line actionBar = (ActionBarActivity)getApplicationContext().getSupportActionBar();
Error
Error:(48, 63) error: cannot find symbol method getSupportActionBar()
Error:(48, 21) error: incompatible types: ActionBarActivity cannot be converted to ActionBar
Let Edit extend AppCompatActivity instead FragmentActivity. AppCompatActivity extends FragmentActivity and adds support for the ActionBar. You can read more here
you need to change FragmentActivity to ActionBarActivity
because this method is not provide in FragmentActivity so change.
public class mainActivity extends ActionBarActivity
check this Link
Replace
public class Edit extends FragmentActivity implements ActionBar.TabListener
with
public class Edit extends ActionBarActivity implements ActionBar.TabListener
and you line
actionBar = (ActionBar)getApplicationContext().getSupportActionBar();
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.
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.
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(); ?
I was recommended to extend my Activity class from ActionBarActivity
Here is the previous code:
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I wrote new application and followed advice.
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If I use ACtionBarActivity instead of Activity, I get the following error on the phone, when I try to run it:
The method getSupportActionBar() is undefined for the type TaskActivity
Your class needs to extend from ActionBarActivity, rather than a plain Activity in order to use the getSupport*() methods.
Update [2015/04/23]: With the release of Android Support Library 22.1, you should now extend AppCompatActivity. Also, you no longer have to extend ActionBarActivity or AppCompatActivity, as you can now incorporate an AppCompatDelegate instance in any activity.
Here is another solution you could have used. It is working in my app.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.support.v7.app.ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_main)
Then you can get rid of that import for the one line ActionBar use.
If you are already extending from ActionBarActivity and you are trying to get the action bar from a fragment:
ActionBar mActionBar = (ActionBarActivity)getActivity()).getSupportActionBar();
Here is the answer of my question.
I've asked that again with some remarks.
How to add support libraries?
If you are extending from an AppCompatActivity and are trying to get the ActionBar from the Fragment, you can do this:
ActionBar mActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
You have to change the extends Activity to extends
AppCompactActivity then try set and getSupportActionBar()
Can you set the ActionBar before you set the Contient View? This order would be better:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}