Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialWilfredo Lopez
3,693 PointsFATAL EXCEPTION: main Process: ava.lang.IllegalStateException: Could not execute method of the activity
Hello!
Been trying to get my IntroActivity.java
to enter my FmMenu.java
(which is a fragment) and I am getting the following error:
02-26 02:10:58.197 25189-25189/com.wlodsgn.bunbunup E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.wlodsgn.bunbunup, PID: 25189
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.wlodsgn.bunbunup/com.wlodsgn.bunbunup.FmMenu}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
at android.app.Activity.startActivityForResult(Activity.java:3736)
at android.app.Activity.startActivityForResult(Activity.java:3697)
at android.app.Activity.startActivity(Activity.java:4007)
at android.app.Activity.startActivity(Activity.java:3975)
at com.wlodsgn.bunbunup.IntroActivity.startMenuActivity(IntroActivity.java:51)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Here is my IntroActivity.java (Where my app start and should enter FmMenu with a button click)
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
/**
* Created by WiLo on 2/13/2015.
*/
public class IntroActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
/*getActionBar().hide();*/
setContentView(R.layout.activity_intro);
Log.i("BunBunUp", "MainActivity Created");
}
/**@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_intro, menu);
return true;
}
@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);
}**/
public void startMenuActivity(View v){
Intent intent = new Intent(IntroActivity.this, FmMenu.class);
startActivity(intent);
}
protected void onResume(){
super.onResume();
Log.i("BunBunUp", "IntroActivity Resumed");
}
protected void onPause(){
super.onPause();
Log.i("BunBunUp", "IntroActivity Paused");
}
protected void onStop(){
super.onStop();
Log.i("BunBunUp", "IntroActivity Stopped");
}
}
FmMenu.java
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by WiLo on 2/13/2015.
*/
public class FmMenu extends Fragment {
String[] categoria = {
"Jeans"
};
int[] imagenes = {
R.drawable.veroxjeans1,
R.drawable.veroxjeans2,
R.drawable.veroxjeans3,
R.drawable.veroxjeans4,
R.drawable.veroxjeans5,
R.drawable.veroxjeans6,
R.drawable.veroxjeans7
};
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lay_menufragment, container, false);
//lista
ListView lista = (ListView) rootView.findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter(this.getActivity(), android.R.layout.simple_list_item_1, categoria);
lista.setAdapter(adapter);
//galeria de imagenes
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[0]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[1]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[2]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[3]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[4]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[5]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[6]));
mViewPager.setAdapter(mSectionsPagerAdapter);
return rootView;
}
/**@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay_menufragment);
}**/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentos;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
fragmentos = new ArrayList<Fragment>();
}
public void addfragments(Fragment xfragment){
fragmentos.add(xfragment);
}
@Override
public Fragment getItem(int position) {
return fragmentos.get(position);
}
@Override
public int getCount() {
return fragmentos.size();
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_IMAGE = "imagen";
private int imagen;
public static PlaceholderFragment newInstance(int imagen) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_IMAGE, imagen);
fragment.setArguments(args);
fragment.setRetainInstance(true);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getArguments() != null) {
imagen = getArguments().getInt(ARG_IMAGE);
}
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
ImageView imagenView = (ImageView) rootView.findViewById(R.id.imageView1);
imagenView.setImageResource(imagen);
return rootView;
}
}
}
MenuActivity.java (which contains navigation drawer and search box)
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.MenuItemCompat.OnActionExpandListener;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
/**
* Created by WiLo on 2/13/2015.
*/
public class MenuActivity extends ActionBarActivity implements OnQueryTextListener, OnActionExpandListener{
/*private TextView texto;*/
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
/*texto = (TextView) findViewById(R.id.texto);*/
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// agregar un nuevo item al menu deslizante
// Menu
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Contacto
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Catologo
//navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1), true, "Estrenos"));
// old Contacto (Pedidos)
//navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
Toast.makeText(getApplicationContext(), "Abriendo Busqueda", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
Toast.makeText(getApplicationContext(), "Cerrando Busqueda", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onQueryTextSubmit(String s) {
/*texto.setText("Buscando...\n\n" + s);*/
return false;
}
@Override
public boolean onQueryTextChange(String s) {
/*texto.setText(" \n\n" + s);*/
return false;
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu3_buscar);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(searchItem, this);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FmMenu();
break;
case 1:
fragment = new FmContacto();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("BunBunUp", "MenuActivity - Error cuando se creo el fragment");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggle
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
Any suggestion would be appreciated
5 Answers
Wilfredo Lopez
3,693 PointsAfter so many hours of trial and error, I was finally able to get the app working under fragments now. The problem consisted in how i had the layout distributed on activity_main.xml
and lay_menufragment.xml
Appreciate your help guys!
Quincy Leito
20,503 PointsA fragment can only live inside a activity, so you can't call an intent to start a fragment. Take a look at the android developers documentation to see more about fragments. below an example on how to call a fragment into an activity;
MainFragment mainFragment = new MainFragment();
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, mainFragment).commit();
Plus you need to place a fragmentcontainer inside your activity_main layout. Like this;
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
So that the fragment can be placed into it.
Wilfredo Lopez
3,693 PointsThanks for the reply Quincy,
I believe I already have that statement in my MenuActivity.java
(You can see it in the code I pasted above)
Here is the snippet:
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
Its inside private void displayView(int position)
or should I add it separately within MenuActivity.java
Harry James
14,780 PointsHey again Wilfredo!
Appreciate the ask to answer :) I'll try my best to help out!
Your problem lies here in IntroActivity.java:
Intent intent = new Intent(IntroActivity.this, FmMenu.class);
startActivity(intent);
You're trying to start a fragment with an Intent and, that can't be done. Intents can only start activities.
There's different ways you can go about making fragments. It looks like you were trying to set up fragments for a ViewPager from the last forum I was talking to you in but, there's also other options such as LinearLayouts as well.
Choose whatever suits you best though, the end result will still be the same :)
Hope it helps :D
Edit: Also, Quincy has shown yet another way of adding fragments (There's loads to choose from!)
Wilfredo Lopez
3,693 PointsChanged the intent so it can go back to MenuActivity.java
and I am going to see what can I do with yours and Quincy's suggestion. I'll let you know if I need more help.
Thanks! :)
Wilfredo Lopez
3,693 PointsOk, so now I did some changes to the project in which IntroActivity.java
will enter MainActivity.java
(Changed the name from old MenuActivity.java) and viewPager
& Listview
are now inside MainActivity.java
Now the issue that I have is that whenever I try to change from Menu fragment in the navigation drawer (Which it shows my pageviewer and listview correctly) to Contacto fragment, it stays in the same page with both pageviewer and listview including a large text I added in lay_contactofragment.xml
to differentiate both fragments.
Here are some screenshots of what happens when I click on both fragment in navigation drawer:
MainActivity.java
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.MenuItemCompat.OnActionExpandListener;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Toast;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
/**
* Created by WiLo on 2/13/2015.
*/
public class MainActivity extends ActionBarActivity implements OnQueryTextListener, OnActionExpandListener{
/*private TextView texto;*/
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
String[] categoria = {
"Jeans"
};
int[] imagenes = {
R.drawable.veroxjeans1,
R.drawable.veroxjeans2,
R.drawable.veroxjeans3,
R.drawable.veroxjeans4,
R.drawable.veroxjeans5,
R.drawable.veroxjeans6,
R.drawable.veroxjeans7
};
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*texto = (TextView) findViewById(R.id.texto);*/
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// agregar un nuevo item al menu deslizante
// Menu
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Contacto
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Catologo
//navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1), true, "Estrenos"));
// old Contacto (Pedidos)
//navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
//lista
ListView lista = (ListView) findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, categoria );
lista.setAdapter(adapter);
//galeria de imagenes
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[0]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[1]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[2]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[3]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[4]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[5]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[6]));
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
Toast.makeText(getApplicationContext(), "Abriendo Busqueda", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
Toast.makeText(getApplicationContext(), "Cerrando Busqueda", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onQueryTextSubmit(String s) {
/*texto.setText("Buscando...\n\n" + s);*/
return false;
}
@Override
public boolean onQueryTextChange(String s) {
/*texto.setText(" \n\n" + s);*/
return false;
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu3_buscar);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(searchItem, this);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FmMenu();
break;
case 1:
fragment = new FmContacto();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("BunBunUp", "MenuActivity - Error cuando se creo el fragment");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggle
mDrawerToggle.onConfigurationChanged(newConfig);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentos;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
fragmentos = new ArrayList<Fragment>();
}
public void addfragments(Fragment xfragment){
fragmentos.add(xfragment);
}
@Override
public Fragment getItem(int position) {
return fragmentos.get(position);
}
@Override
public int getCount() {
return fragmentos.size();
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_IMAGE = "imagen";
private int imagen;
public static PlaceholderFragment newInstance(int imagen) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_IMAGE, imagen);
fragment.setArguments(args);
fragment.setRetainInstance(true);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getArguments() != null) {
imagen = getArguments().getInt(ARG_IMAGE);
}
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
ImageView imagenView = (ImageView) rootView.findViewById(R.id.imageView1);
imagenView.setImageResource(imagen);
return rootView;
}
}
}
Left FmMenu and FmContacto with public View onCreateView
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by WiLo on 2/13/2015.
*/
public class FmMenu extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lay_menufragment, container, false);
return rootView;
}
}
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by WiLo on 2/13/2015.
*/
public class FmContacto extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lay_contactofragment, container, false);
return rootView;
}
}
And activity_menu.xml with both viewPager and Listview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0a393d">
<!-- Linearlayout to display Fragments -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/container">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="265dp"
tools:context=".MenuActivity" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView1"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>
Wilfredo Lopez
3,693 PointsHey Harry, I've been trying to look for a proper way to create a Filter By activity from my ListView Volley using JSON but don't know where to start. Here is the thread I created. Any guidance would be wonderful :)
Thanks!
Harry James
14,780 PointsHey Wilfredo!
I'm working on sending you an example for how you can filter items for a ListView and should get it to you as soon as possible (This will likely be tomorrow or Monday, however).
I've just recently finished some of my exams so I'm just getting back into things again so please bare with me and I just wanted to let you know that I have got your message and will get back to you :)
Speak to you soon!
Wilfredo Lopez
3,693 PointsThanks for the reply Harry!
Appreciate the time in responding with all the stuff you are doing. Looking forward to your example :D