{"id":1825,"date":"2022-08-30T15:19:45","date_gmt":"2022-08-30T15:19:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/fragment-cannot-be-resolved-to-a-type-after-adding-support-library-in-android-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:45","modified_gmt":"2022-08-30T15:19:45","slug":"fragment-cannot-be-resolved-to-a-type-after-adding-support-library-in-android-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/fragment-cannot-be-resolved-to-a-type-after-adding-support-library-in-android-collection-of-common-programming-errors\/","title":{"rendered":"Fragment cannot be resolved to a type after adding support library in android-Collection of common programming errors"},"content":{"rendered":"<p>Hi I want to use Fragment and action bar in my project for android 2.2 so I had added Support Library- v4 and v7 in but still I am getting error as<\/p>\n<blockquote>\n<p>Fragment cannot be resolved to a type and The method getActionBar() is undefined for the type MainActivity.<\/p>\n<\/blockquote>\n<p>here is my code:<\/p>\n<pre><code>import java.util.Locale;\n\nimport android.R;\nimport android.app.SearchManager;\nimport android.content.Intent;\nimport android.content.res.Configuration;\nimport android.os.Bundle;\nimport android.support.v4.app.ActionBarDrawerToggle;\nimport android.support.v4.app.Fragment;\nimport android.support.v4.app.FragmentManager;\nimport android.support.v4.view.GravityCompat;\nimport android.support.v4.widget.DrawerLayout;\nimport android.support.v7.app.ActionBarActivity;\nimport android.view.LayoutInflater;\nimport android.view.Menu;\nimport android.view.MenuInflater;\nimport android.view.MenuItem;\nimport android.view.View;\nimport android.view.ViewGroup;\nimport android.widget.AdapterView;\nimport android.widget.ArrayAdapter;\nimport android.widget.ImageView;\nimport android.widget.ListView;\nimport android.widget.Toast;\nimport android.support.v7.appcompat.*;\nimport android.support.v7.app.ActionBar;\n\npublic class MainActivity extends ActionBarActivity {\nprivate DrawerLayout mDrawerLayout;\nprivate ListView mDrawerList;\nprivate ActionBarDrawerToggle mDrawerToggle;\n\nprivate CharSequence mDrawerTitle;\nprivate CharSequence mTitle;\nprivate String[] mPlanetTitles;\n\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_main);\n\n    mTitle = mDrawerTitle = getTitle();\n    mPlanetTitles = getResources().getStringArray(R.array.planets_array);\n    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);\n    mDrawerList = (ListView) findViewById(R.id.left_drawer);\n\n    \/\/ set a custom shadow that overlays the main content when the drawer\n    \/\/ opens\n    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,\n            GravityCompat.START);\n    \/\/ set up the drawer's list view with items and click listener\n    mDrawerList.setAdapter(new ArrayAdapter(this,\n            R.layout.drawer_list_item, mPlanetTitles));\n    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());\n\n    \/\/ enable ActionBar app icon to behave as action to toggle nav drawer\n    getActionBar().setDisplayHomeAsUpEnabled(true);\n    getSupportActionbar().setHomeButtonEnabled(true);\n\n    \/\/ ActionBarDrawerToggle ties together the the proper interactions\n    \/\/ between the sliding drawer and the action bar app icon\n    mDrawerToggle = new ActionBarDrawerToggle(this, \/* host Activity *\/\n    mDrawerLayout, \/* DrawerLayout object *\/\n    R.drawable.ic_drawer, \/* nav drawer image to replace 'Up' caret *\/\n    R.string.drawer_open, \/* \"open drawer\" description for accessibility *\/\n    R.string.drawer_close \/* \"close drawer\" description for accessibility *\/\n    ) {\n        public void onDrawerClosed(View view) {\n            getSupportActionbar().setTitle(mTitle);\n            invalidateOptionsMenu(); \/\/ creates call to\n                                        \/\/ onPrepareOptionsMenu()\n        }\n\n        public void onDrawerOpened(View drawerView) {\n            getSupportActionbar().setTitle(mDrawerTitle);\n            invalidateOptionsMenu(); \/\/ creates call to\n                                        \/\/ onPrepareOptionsMenu()\n        }\n    };\n    mDrawerLayout.setDrawerListener(mDrawerToggle);\n\n    if (savedInstanceState == null) {\n        selectItem(0);\n    }\n}\n\n@Override\npublic boolean onCreateOptionsMenu(Menu menu) {\n    MenuInflater inflater = getMenuInflater();\n    inflater.inflate(R.menu.main, menu);\n    return super.onCreateOptionsMenu(menu);\n}\n\n\/* Called whenever we call invalidateOptionsMenu() *\/\n@Override\npublic boolean onPrepareOptionsMenu(Menu menu) {\n    \/\/ If the nav drawer is open, hide action items related to the content\n    \/\/ view\n    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);\n    menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);\n    return super.onPrepareOptionsMenu(menu);\n}\n\n@Override\npublic boolean onOptionsItemSelected(MenuItem item) {\n     \/\/ The action bar home\/up action should open or close the drawer.\n     \/\/ ActionBarDrawerToggle will take care of this.\n    if (mDrawerToggle.onOptionsItemSelected(item)) {\n        return true;\n    }\n    \/\/ Handle action buttons\n    switch(item.getItemId()) {\n    case R.id.action_websearch:\n        \/\/ create intent to perform web search for this planet\n        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);\n        intent.putExtra(SearchManager.QUERY, getSupportActionbar()().getTitle());\n        \/\/ catch event that there's no activity to handle intent\n        if (intent.resolveActivity(getPackageManager()) != null) {\n            startActivity(intent);\n        } else {\n            Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();\n        }\n        return true;\n    default:\n        return super.onOptionsItemSelected(item);\n    }\n}\n\n\/* The click listner for ListView in the navigation drawer *\/\nprivate class DrawerItemClickListener implements\n        ListView.OnItemClickListener {\n    @Override\n    public void onItemClick(AdapterView parent, View view, int position,\n            long id) {\n        selectItem(position);\n    }\n}\n\nprivate void selectItem(int position) {\n    \/\/ update the main content by replacing fragments\n    Fragment fragment = new PlanetFragment();\n    Bundle args = new Bundle();\n    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);\n    fragment.setArguments(args);\n\n    FragmentManager fragmentManager = getFragmentManager();\n    fragmentManager.beginTransaction()\n            .replace(R.id.content_frame, fragment).commit();\n\n    \/\/ update selected item and title, then close the drawer\n    mDrawerList.setItemChecked(position, true);\n    setTitle(mPlanetTitles[position]);\n    mDrawerLayout.closeDrawer(mDrawerList);\n}\n\n@Override\npublic void setTitle(CharSequence title) {\n    mTitle = title;\n    getSupportActionbar().setTitle(mTitle);\n}\n\n\/**\n * When using the ActionBarDrawerToggle, you must call it during\n * onPostCreate() and onConfigurationChanged()...\n *\/\n\n@Override\nprotected void onPostCreate(Bundle savedInstanceState) {\n    super.onPostCreate(savedInstanceState);\n    \/\/ Sync the toggle state after onRestoreInstanceState has occurred.\n    mDrawerToggle.syncState();\n}\n\n@Override\npublic void onConfigurationChanged(Configuration newConfig) {\n    super.onConfigurationChanged(newConfig);\n    \/\/ Pass any configuration change to the drawer toggls\n    mDrawerToggle.onConfigurationChanged(newConfig);\n}\n\n\/**\n * Fragment that appears in the \"content_frame\", shows a planet\n *\/\npublic static class PlanetFragment extends Fragment {\n    public static final String ARG_PLANET_NUMBER = \"planet_number\";\n\n    public PlanetFragment() {\n        \/\/ Empty constructor required for fragment subclasses\n    }\n\n    @Override\n    public View onCreateView(LayoutInflater inflater, ViewGroup container,\n            Bundle savedInstanceState) {\n        View rootView = inflater.inflate(R.layout.fragment_planet,\n                container, false);\n        int i = getArguments().getInt(ARG_PLANET_NUMBER);\n        String planet = getResources()\n                .getStringArray(R.array.planets_array)[i];\n\n        int imageId = getResources().getIdentifier(\n                planet.toLowerCase(Locale.getDefault()), \"drawable\",\n                getActivity().getPackageName());\n        ((ImageView) rootView.findViewById(R.id.image))\n                .setImageResource(imageId);\n        getActivity().setTitle(planet);\n        return rootView;\n    }\n}\n}\n<\/code><\/pre>\n<p>Now here I had tried both<\/p>\n<p><code>getSupportActionbar()<\/code> and and <code>getActionBar()<\/code>. But both gives error.<\/p>\n<p>Also I have <code>activity_main.xml<\/code> file but in <code>setContentView(R.layout.activity_main);<\/code> also it gives error as <code>activity_main<\/code> cannot be resolved or is not a field`<\/p>\n<p>I had already setup compact library file in below screen shot<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/jILF5.png\" \/><\/p>\n<ol>\n<li>\n<ol>\n<li>\n<p>You need to use <code>getSupportActionBar()<\/code> instead of <code>getActionBar()<\/code> if you&#8217;re using the support library.<\/p>\n<\/li>\n<li>\n<p>Make sure your <code>Fragment<\/code> is from <code>android.support.v4.app.Fragment<\/code> namespace &#8211; this one is from the support library.<\/p>\n<\/li>\n<\/ol>\n<\/li>\n<li>\n<p>Right click on the action bar Sherlock library =&gt; Properties =&gt; Java Build Path =&gt; Order and Export tab =&gt; check android support v4 or Android Private Libraries and select OK Right click on your personal project and go to properties =&gt; java build path =&gt; check Android Private Libraries Clean both projects, it should work.<\/p>\n<\/li>\n<li>\n<p>If supporting API levels <strong>lower than 11<\/strong>: <code>import android.support.v7.app.ActionBar<\/code><\/p>\n<p>Also your fragment import must be from support library <code>import android.support.v4.app.Fragment<\/code><\/p>\n<p>Use <code>getSupportActionBar()<\/code> instead of <code>getActionBar()<\/code>. Your activity must extend <code>ActionBarActivity<\/code>.<\/p>\n<p>In Fragment<\/p>\n<pre><code>((ActionBarActivity) getActivity()).getSupportActionBar().setTitle(\"My Title\"); \n<\/code><\/pre>\n<p>Check the topic adding the action bar<\/p>\n<p>http:\/\/developer.android.com\/guide\/topics\/ui\/actionbar.html<\/p>\n<p>Edit:<\/p>\n<p>CHange<\/p>\n<pre><code> getActionBar().setDisplayHomeAsUpEnabled(true);\n<\/code><\/pre>\n<p>to<\/p>\n<pre><code> getSupportActionBar().setDisplayHomeAsUpEnabled(true);\n<\/code><\/pre>\n<p>Remove<\/p>\n<pre><code> import android.R;\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 06:13:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Hi I want to use Fragment and action bar in my project for android 2.2 so I had added Support Library- v4 and v7 in but still I am getting error as Fragment cannot be resolved to a type and The method getActionBar() is undefined for the type MainActivity. here is my code: import java.util.Locale; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1825","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1825"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1825\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}