Error on newInstance() using fragments - android

I'm trying to use the navigation bar that comes pre-built in Android Studio and found several guides on how to use it but most of them are either outdated or for some reason don't work.
The one I use now seems to "work" but I get an error on newInstance()
Error code:
Error:(58, 59) error: method newInstance in class FirstFragment cannot be applied to given types;
required: String,String
found: no arguments
reason: actual and formal argument lists differ in length
Here is my code:
`package welovecode.se.outfitted;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
if (position == 0) {
fragmentManager.beginTransaction()
.replace(R.id.container, FirstFragment.newInstance())
.commit();
} else if (position == 1) {
fragmentManager.beginTransaction()
.replace(R.id.container, SecondFragment.newInstance())
.commit();
} else if (position == 2) {
fragmentManager.beginTransaction()
.replace(R.id.container, ThirdFragment.newInstance())
.commit();
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
`
Thank you so much in advance!

The error is pretty informative. You have classes FirstFragment, SecondFragment etc which define newInstance methods that take arguments, in this case two strings. You are however calling those methods without any arguments.
So the solution should be obvious, either supply the needed arguments, or modify those classes not to require any arguments. It's hard to tell you what exactly you should change in your code because 1) I don't know exactly what you want to accomplish and 2) I don't have the code for your fragments.

It seems to me you want to pass a Section Number for your Fragment, like:
int sectNum = 100;
fragmentManager.beginTransaction()
.replace(R.id.container, FirstFragment.newInstance( sectNum ))
Notes:
newInstance method call requires an int type only.
You need to determine how to get the section number.

Related

NullPointerException ViewPager SetAdapter

I am adding a tab view or (ViewPager) to my Fragment in an App I am working on.
I want to put the tab view inside the Contact Fragment
I have a Navigation Drawer that opens, and you see list of options...
Home
Services
Contact
Everything you click goes to a fragment
Then in that fragment loads the .XML file, with a corresponding class to program everything inside the .XML file.
I was following an online tutorial [CLICK HERE][3]
Obviously it was over 2 years ago. Android Code has changed since then. I started with a few more errors then what I have now.
I am experiencing an issue with getSupportFragmentManager(), and getActionBar() really confused at this point, all imports are there. It won't recognize those two. Obviously that makes me think well something is missing, but I am not sure what needs to put to allow the compiler to see those needed methods.
I still get Null when the ViewPager setAdapter is set.
All I am trying to accomplish is a tab view, with two tabs for Contact, and Social Media. If I could get a simplistic understanding of what is required for tabs to work properly.
I have already searched questions and they don't exactly touch on my specific issue, and so I am asking for help. Any help is definitely appreciated.
FragmentContact
package com.megliosolutions.bitsnbytes.Fragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.megliosolutions.bitsnbytes.MainActivity;
import com.megliosolutions.bitsnbytes.R;
import com.megliosolutions.bitsnbytes.ViewPagerClasses.TabsPagerAdapter;
/**
* Created by Jarvis on 6/21/15.
*/
public class FragmentContact extends Fragment {
private ViewPager viewPager;
TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = {"Contact", "Social Media"};
public static FragmentContact newInstance(){
FragmentContact fragment = new FragmentContact();
return fragment;
}
public FragmentContact(){};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contact, container, false);
LoadTabs(rootView);
return rootView;
}
private void LoadTabs(View rootView) {
FragmentManager fragmentManager = getSupportFragmentManager();
// Initilization
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(fragmentManager);
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener((ActionBar.TabListener) this));
}
}
#Override
public void onAttach(FragmentActivity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(5);
}
}
TabsPagerAdapter
package com.megliosolutions.bitsnbytes.ViewPagerClasses;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
/**
* Created by Jarvis on 6/28/15.
*/
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Contact fragment activity
return new ContactFragment();
case 1:
// Social Media fragment activity
return new SocialMediaFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
}
MAINACTIVITY
package com.megliosolutions.bitsnbytes;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.megliosolutions.bitsnbytes.Fragments.FragmentAbout;
import com.megliosolutions.bitsnbytes.Fragments.FragmentContact;
import com.megliosolutions.bitsnbytes.Fragments.FragmentHome;
import com.megliosolutions.bitsnbytes.Fragments.FragmentProducts;
import com.megliosolutions.bitsnbytes.Fragments.FragmentServices;
import com.megliosolutions.bitsnbytes.Fragments.FragmentWarranties;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
if(position == 0) {
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentHome.newInstance())
.commit();
}
else if(position == 1){
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentServices.newInstance())
.commit();
}
else if(position == 2){
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentProducts.newInstance())
.commit();
}
else if(position == 3){
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentWarranties.newInstance())
.commit();
}
else if(position == 4){
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentContact.newInstance())
.commit();
}
else if(position == 5){
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentAbout.newInstance())
.commit();
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
case 4:
mTitle = getString(R.string.title_section4);
break;
case 5:
mTitle = getString(R.string.title_section5);
break;
case 6:
mTitle = getString(R.string.title_section6);
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
Now there is an issue with the FragmentContact.newInstance() in onNavigationDrawerItemSelected()
06-28 15:35:24.695 11744-11744/com.megliosolutions.bitsnbytes E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.megliosolutions.bitsnbytes, PID: 11744
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at com.megliosolutions.bitsnbytes.Fragments.FragmentContact.LoadTabs(FragmentContact.java:55)
at com.megliosolutions.bitsnbytes.Fragments.FragmentContact.onCreateView(FragmentContact.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
from logcat Error its clear ViewPager not exist in R.layout.fragment_contact. Please verify that.
Since I need to refer to the viewPager in which it exists on activity_main.xml not the current fragment, I manage to solve this error:
I added a declaration for a new view referring to the activity where the ViewPager exists as following:
View viewNext = inflater.inflate(R.layout.activity_main, container, false);
Then, I declared the viewPager with regards the above declaration:
viewPager = (ViewPager) viewNext.findViewById(R.id.viewPager);
After that, I set up the adaptor.

Changing fragment from Navigation drawer

I am basically having the exact same problem as OP here: How to change fragments using Android navigation drawer
Also using the Template for an navigation drawer activity in Android Studio.
I tried Dreagan's answer, but I am now stuck with a cycle of errors.
Here is my code
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = new NewsfeedFragment();
FragmentManager fragmentManager = getFragmentManager();
Bundle args = new Bundle();
args.putInt(NewsfeedFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
switch(position){
case 0:
fragment = new NewsfeedFragment();
break;
case 1:
fragment = new NewsfeedFragment();
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
If I run the code as it is here I get following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{dk.gruppe2.jammerbugtevent/dk.gruppe2.jammerbugtevent.MainActivity}: java.lang.ClassCastException: dk.gruppe2.jammerbugtevent.MainActivity#429e4a70 must implement OnFragmentInteractionListener
When I implement OnFragmentInteractionListener, I get the following error:
Error:(22, 9) error: MainActivity is not abstract and does not override abstract method onFragmentInteraction(Uri) in OnFragmentInteractionListener
When i declare MainActivity as abstract, i get this error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{dk.gruppe2.jammerbugtevent/dk.gruppe2.jammerbugtevent.MainActivity}: java.lang.InstantiationException: can't instantiate class dk.gruppe2.jammerbugtevent.MainActivity
I am very new to android, and not exactly an expert regarding programming to begin with. Any help would be much appreciated. Thanks In advance.
Here is an explanation of things!
Reading your code i did not see any class cast exception... You are casting your activity to the interface OnFragmentInteractionListener. Maybe in one of the fragments.
Also:
When I implement OnFragmentInteractionListener, I get the following
error:
Error:(22, 9) error: MainActivity is not abstract and does not
override abstract method onFragmentInteraction(Uri) in
OnFragmentInteractionListener
When you implement the OnFragmentInteractionListener you must also Override the method: onFragmentInteraction(Uri).
It looks like you aren't setting the fragment's correctly:
#Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch(position)
{
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, MainListFragment.newInstance())
.commit();
break;
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, Fragment2.newInstance())
.commit();
case 2:
fragmentManager.beginTransaction()
.replace(R.id.container, Fragment3.newInstance())
.commit();
break;
}
}
Try using this format, as it stands, you aren't using the new instance method you've defined in the fragment.
I found that I only implemented the interfaces, but still needed to implement the methods from the interfaces

Incompatible types: Fragment cannot be converted to NavigationDrawerFragment

I've been working on an app using API 14, but I had to downgrade to API 10. the app has fragments and activity fragments, I have imported the libraries and used the getFragmentSupportManager because it is v4. However, I have following error:
I created a dummy app with Drawer layout and fragments with API 10 from the beginning, it had the same line in MyActivity file and it was not an error. Here is my code:
package az.test2;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
public class MyActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
android.support.v4.app.FragmentManager lolo;
FragmentManager FM = getSupportFragmentManager();
FragAV fav = new FragAV();
FragW fw = new FragW();
switch (position){
case 0:
FM.beginTransaction()
.replace(R.id.container, fav).commit();
break;
case 1:
FM.beginTransaction()
.replace(R.id.container, fw).commit();
break;
case 2: break;
}
//FM.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position + 1)).commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.my, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MyActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
I did everything I could but this error seems to be very rare. any help would be much appreciated.
When using the support pack, your fragments should extend android.support.v4.app.Fragment and not the regular Fragment class.
make sure your drawer fragment class extend
android.support.v4.app.Fragment
not fragment.
change this
NavigationDrawerFragment mNavigationDrawerFragment;
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
to
Fragment mNavigationDrawerFragment;
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
in your mNavigationDrawerFragment class import
"import android.support.v4.app.Fragment;"
instead of "import android.app.Fragment;"

Fragment error in android navigation drawer

Hello I'm getting expression expected error multiple times and I don't know what i'm doing wrong, if someone can help me that will be great.
Thanks.
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.app.ActionBar;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, Fragment) //expression expected error on "Fragment"
.commit();
}
public void onSectionAttached(int position) {
switch (position) {
case 1:
Fragment = new Welcome(); //expression expected error on "Fragment"
break;
case 2:
Fragment = new Tutorials(); //expression expected error on "Fragment"
break;
case 3:
Fragment = new Blueprints(); //expression expected error on "Fragment"
break;
case 4:
Fragment = new Wiki(); //expression expected error on "Fragment"
break;
case 5:
Fragment = new Chat(); //expression expected error on "Fragment"
break;
case 6:
Fragment = new Item_Search(); //expression expected error on "Fragment"
break;
case 7:
Fragment = new Recipes(); //expression expected error on "Fragment"
break;
case 8:
Fragment = new Wallpaper(); //expression expected error on "Fragment"
break;
case 9:
Fragment = new Maps_Mods(); //expression expected error on "Fragment"
break;
default :
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
The error is underlining "Fragment". I'm trying to have the navigation drawer stitch to different fragments.
It should be
fragmentManager.beginTransaction()
.replace(R.id.container,new Welcome())
and
Welcome fragment = new Welcome();
Similarly for others.
Since you are using support based Fragment. You need to change this
public class MainActivity extends Activity
to
public class MainActivity extends ActionBarActivity
if you need ActionBar. You also need to use AppCompat.
Check this
Navigation Drawer with backword compatibility android
Fragment is a key word. What you need is a variable of type Fragment. So, for example, put this in the same section as mTitle:
Fragment mFragment = null;
In your switch statement, put, for example:
mFragment = new Welcome();
Then, when you're setting up the transaction, you can put:
fragmentManager.beginTransaction()
.replace(R.id.container, mFragment)
This may not solve all your problems, but should get you past these compilation errors.

how do i give each fragment its own view rather then 1 view different number?

im using android studio, ive created a project using navigation drawer with fragments,
as you may/may not know it gives default code to show 1 view with the fragment page number for each page u click.
my problem or rather query is how do i give each fragment its own view.
i want to create 3 possibly 4 layout.xml files but want each of the options in the drawer to open its own xml.
i hope this makes sense and here is my code
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setTitle(mTitle);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#DEDBA7")));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
Now im guessing this section is the code that places the number in each fragment, but i want to get rid of the number and have a different view for each item i click.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
You need to either create different fragments for each and then select the correct fragment you want in onNavigationDrawerItemSelected.
Or you need to pass a meaningful argument to PlaceHolder fragment that will allow you to pick the correct layout and inflate it, in onCreateView.

Categories

Resources