Android MyAdapter can't be static - android

I am having some trouble with this piece of code:
MyAdapter.java:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: return new SpeedZonesFragment();
case 1: return new DistanceFragment();
case 2: return new FuelConsumptionFragment();
default: return null;
}
}
}
MainActivity.java:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
private MyAdapter myAdapter;
private ViewPager myViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager);
myAdapter = new MyAdapter(getSupportFragmentManager());
myViewPager = (ViewPager) findViewById(R.id.pager);
myViewPager.setAdapter(myAdapter);
view_pager.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_pager" />
</LinearLayout>
My problem goes that every example i can find declares MyAdapter as a public static class however Eclipse raise the error: Illegal modifier for the class MyAdapter; only public, abstract & final are permitted.
If i try to run my code without the static it returns (from LogCat):
Caused by: java.lang.NullPointerException
02-02 23:13:26.076: E/AndroidRuntime(928):
at simcas.fartberegneren.MainActivity.onCreate(MainActivity.java:23)
EDIT: Line 23 is the line myViewPager.setAdapter(myAdapter);
Clearly i am doing something wrong but i just can't locate it.

Firstly, no, static classes are only used for inner classes wherein the inner class's instances should not be tied to the outer class's instance. (That is, in an outer class, the static modified should not be used.)
Second, the null pointer is in the statement myViewPager.setAdapter(myAdapter);, as you deduced, meaning myViewPager is null. Therefore, the findViewById() is returning null (item not found):
myViewPager = (ViewPager) findViewById(R.id.pager);
Notice, in your XML: android:id="#+id/view_pager". The above line should then be:
myViewPager = (ViewPager) findViewById(R.id.view_pager);

Related

How to do SharedElement Transition with Viewpager between Fragments

I have researched a lot on SharedElement Transitions , however I could only find Transitions for onclick event.
I want the animation between Fragments where the speed of animation is controlled by the user's scrolling speed. If the user stops in between while scrolling the objects should be still.
Example :
In this image , the viewpager is in the middle of screen1 and screen2 and the animating objects are in stopped state.
These objects move from one screen to another and also change their positions.
The translation animations part I have figured out but how to achieve this shared element transition with viewpager between fragments. And moreover it should scroll/animate with the speed of finger swipe.
MainActivity.java
package com.karan.onboardanimation;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private MyFragmentPagerAdapter pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: ");
ViewPager pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
pagerAdapter = new MyFragmentPagerAdapter(fm);
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(0);
}
}
FirstFragment.java- This fragment has an imageview which has to shared with second fragment when user is swiping on the viewpager.
package com.karan.onboardanimation;
import android.app.ActivityOptions;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class FirstFragment extends Fragment {
private static final String TAG = "FirstFragment";
ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= 21){
}
Log.d(TAG, "onCreate: ");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: ");
View view = inflater.inflate(R.layout.first_fragment, container, false);
imageView = (ImageView) view.findViewById(R.id.img1);
ViewPagerTransformer viewPagerTransformer = new ViewPagerTransformer();
viewPagerTransformer.transformPage(imageView, 1);
return view;
}
}
MyFragmentPagerAdapter.java
package com.karan.onboardanimation;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private static final String TAG = "MyFragmentPagerAdapter";
final int PAGE_COUNT = 2;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
Log.d(TAG, "MyFragmentPagerAdapter: ");
}
#Override
public Fragment getItem(int position) {
Log.d(TAG, "getItem: ");
switch (position) {
case 0:
return new FirstFragment();
case 1:
return new SecondFragment();
default:
return null;
}
}
#Override
public int getCount() {
return PAGE_COUNT;
}
}
second_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d02030"
android:orientation="vertical">
<ImageView
android:id="#+id/img2"
android:layout_width="100dp"
android:layout_centerInParent="true"
android:src="#drawable/orange_centre"
android:transitionName="selectedIcon"
android:layout_height="100dp" />
</RelativeLayout>
first_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/img1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="#drawable/orange_centre" />
</LinearLayout>
Edit1-
This question is not a duplicate of link mentioned in comment because that question is a sub part of my question. The difference is that the animation in that question happens on a click with a static speed. However in my question, the speed of animation must be equal to the speed of ViewPager swipe speed.
You can try PageTransformer. It is not shared transition but more of view animation that is dependent on page position offset generated by swipe events.

Android ViewPager keeps crashing with NullPointerException on orientation change

I am trying to add a ViewPager to my Android app as a one-time setup screen for the user. But the problem I am facing is that the app keeps crashing when the orientation of the screen is changed amidst the setup process.
If the app is opened in either portrait or landscape mode and used without any orientation change, it works fine. But if the orientation is changed during runtime, using the method setCurrentItem(int position) on the ViewPager crashes the app.
Here's my Fragment class -
package com.cosine.arc;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class WelcomeFragment extends Fragment {
private int mPosition;
private Context mContext;
private ViewPager mPager;
private final int[] welcomeFragments = {R.layout.fragment_welcome1};
public WelcomeFragment() {
// Required empty public constructor
}
public WelcomeFragment(Context context, ViewPager viewPager, int position) {
this.mPosition = position;
this.mContext = context;
this.mPager = viewPager;
}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = null;
try {
switch (mPosition) {
case 0:
view = inflater.inflate(R.layout.fragment_welcome1, container, false);
Typeface robotoLight = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Light.ttf");
TextView welcomeTxt1x2 = (TextView) view.findViewById(R.id.welcome_text_1_2);
Button startButton = (Button) view.findViewById(R.id.welcome_btn_1_1);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mPager.setCurrentItem(1);
}
});
welcomeTxt1x2.setTypeface(robotoLight);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
return view;
}
}
And here's my FragmentActivity class with the FragmentStatePagerAdapter class within it -
package com.cosine.arc;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
public class IntroActivity extends FragmentActivity {
private static int NUM_PAGES = 3;
private NonSwipeableViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
mPager = (NonSwipeableViewPager) findViewById(R.id.intro_pager);
mPagerAdapter = new IntroSliderAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem()==0) {
super.onBackPressed();
}
else {
mPager.setCurrentItem(mPager.getCurrentItem()-1);
}
}
public int getCurrentItem() {
return mPager.getCurrentItem();
}
public NonSwipeableViewPager getPagerUpdate() {
return mPager;
}
private class IntroSliderAdapter extends FragmentStatePagerAdapter {
public IntroSliderAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new WelcomeFragment(getBaseContext(), mPager, position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
And here is the error log -
04-07 15:25:13.774 12186-12186/com.cosine.arc E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cosine.arc, PID: 12186 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setCurrentItem(int)' on a null object reference
at com.cosine.arc.WelcomeFragment$1.onClick(WelcomeFragment.java:58)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22288)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
fragment_welcome1.xml -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.cosine.arc.WelcomeFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<TextView
android:id="#+id/welcome_text_1_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/welcome_to_"
android:textColor="#android:color/white"
android:textSize="36sp"
android:padding="16dp"
android:layout_marginTop="16dp"
android:gravity="center"/>
<ImageView
android:id="#+id/welcome_img_1_1"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/ic_logo"
android:layout_below="#id/welcome_text_1_1"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/welcome_text_1_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/welcome_img_1_1"
android:text="#string/lets_get_things_started_"
android:textSize="42sp"
android:textColor="#android:color/white"
android:padding="16dp"
android:layout_marginTop="32dp"/>
<Button
android:id="#+id/welcome_btn_1_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#android:color/white"
android:text="Start"
android:textColor="#android:color/black"
android:layout_margin="16dp"
android:layout_alignParentRight="true"
android:drawableRight="#mipmap/ic_arrow_right_light"/>
</RelativeLayout>
Please do note that I have two different layout files fragment_welcome1.xml and fragment_welcome1.xml-land.
On orientation change, you will have to save the instance of the fragment if you want to retain the references. Have a look at this answer, hope it helps you. In a nutshell, i think you will need to save the instance of the fragment in your container activity's onSaveInstanceState(), and then recreate your saved fragment when orientation changes.
Please note that making config changes in the manifest is not the recommended way to save the instance of the fragment. Making config changes will lead to memory leaks.

Using fragments for TAB layout, how to use them?

I am using fragments to get the Tabs style on my app, like seen here:
Tabs
Already deducted that each tab is a fragment and each one has a layout, but i cant display on each one display what i want.
Before having the fragments i had a simple activity to display data from a database on a listview, but now i cant get it to work on one of the fragments, with the following code that i used:
dal = new DAL(TabListas.this);
dal.connect(DBAccessMode.READ);
Cursor cursor = dal.selectALLFromRegSintomas();
// Find ListView to populate
ListView lvItems = (ListView) findViewById(R.id.listv);
// Setup cursor adapter using cursor from last step
RegistosCursorAdapter todoAdapter = new RegistosCursorAdapter(TabListas.this, cursor, 0);
// Attach cursor adapter to the ListView
lvItems.setAdapter(todoAdapter);
How can i make a use the fragments to display data from db in a listview in one fragment ? What should i change ?
VerRegsSintomas.java (like mainactivity.java):
package com.example.bugdroid.menuexe.Activities;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import com.example.bugdroid.menuexe.CursorAdapter.RegistosCursorAdapter;
import com.example.bugdroid.menuexe.R;
import com.example.bugdroid.menuexe.TabFragments.PageAdapter;
import com.example.bugdroid.menuexe.database.DAL;
import com.example.bugdroid.menuexe.database.DBAccessMode;
public class VerRegsSintomas extends AppCompatActivity {
private ViewPager mViewPager;
private DAL dal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ver_regs_sintomas);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1a212c")));
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Sintomas"));
tabLayout.addTab(tabLayout.newTab().setText("Listas"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PageAdapter adapter = new PageAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
PageAdapter.java:
package com.example.bugdroid.menuexe.TabFragments;
/**
* Created by BugDroid on 07/06/2016.
*/
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PageAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PageAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabSintomas tab1 = new TabSintomas();
return tab1;
case 1:
TabListas tab2 = new TabListas();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
TabSintomas.java:
package com.example.bugdroid.menuexe.TabFragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.bugdroid.menuexe.R;
public class TabSintomas extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab_sintomas, container, false);
}
}
fragment_Tab_sintomas.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Activities.VerRegistos">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listv"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Try this Example ,here you can find a TabLayout with the help of Viewpager and custom toolbar and along with this all you can find a navigation drawer over there.

Android ListView in a ListFragment

I am trying to implement a simple ListView into a Fragment. I have seen many tutorials on how to do so but for some reason my adaptation does not work and I cannot figure out why.
the goal: to have a fragment with one listview.
I understand there are many questions about this and I have seen most of them but mine does not work and I have tried many variations with no success.
From what I have gathered ListFragment and AppCompatActivity are the most appropiate:
//code for fragment.
package com.example.student.hwapp;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class TasksFragment extends ListFragment {
public TasksFragment(){}
public String[] list = {"1","2","3","4","5","6"};
ListAdapter theAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.tasks_fragment,container,false);
theAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, list);
setListAdapter(theAdapter);
return rootView;
}
public static TasksFragment newInstance() {
TasksFragment fragment = new TasksFragment();
return fragment;
}
}
the XML for tasks_fragment (as you can see the list id is list) :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list"
tools:listitem="#android:layout/simple_list_item_2"
android:choiceMode="multipleChoice"
android:footerDividersEnabled="false"
android:clickable="false" />
</RelativeLayout>
and finally the Activity that holds the fragment:
package com.example.student.hwapp;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainMenu extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
return TasksFragment.newInstance();
case 1:
return ClassFragment.newInstance();
}
return null;
}
#Override
public int getCount() {
// Show 2 total pages.
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
}
return null;
}
}
When I test the TasksFragment as a normal activity, it actually works but when I make it a fragment it doesn't.
Thank you for your help, sorry for the very specific and repetitive question but I really could not find a solution, I'm probably missing something very simple.
EDIT:
14203/com.example.student.hwapp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
But I DO have a ListView and its id IS list as I showed in the code.
Your android:id="#+id/list" is not the same as what is being asked for in the error message, which is android:id="#android:id/list". The former is YOUR object; Android needs its OWN object. Refer to this, among many others.

How to add a project ActionBar ViewPage Swipe using SupportLibrary AppcomptV7?

The question is not as complex as it seems.
I made this design based on some tutorials on the internet.
I have a tabbed browsing with swipe using SupportLibrary for devices prior to Honeycomb can visualize.
Now I want to add a ActionBar in the application. Also by using the SuportLibrary appcompat.v7.
I've used the appcompat.v7 to create an application with ActionBar compatible with older versions of Android.
What I'm not getting now and join these two navigational structures.
I really want to add to this mu ActionBar project with swipe tabs below:
MainActivity:
package br.com.wgbn.poemadodiav2;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private ActionBar actionBar;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());
mViewPager.setAdapter(titleAdapter);
mViewPager.setCurrentItem(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
TitleAdapter:
package br.com.wgbn.poemadodiav2;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
public class TitleAdapter extends FragmentPagerAdapter{
private String titles[] = new String[]{"O Poema do Dia","Comentários"};
private Fragment frags[] = new Fragment[titles.length];
public TitleAdapter(FragmentManager fm) {
super(fm);
frags[0] = new FragmentView1();
frags[1] = new FragmentView2();
}
#Override
public CharSequence getPageTitle (int position){
Log.v("TitleAdapter - getPageTitle=", titles[position]);
return titles[position];
}
#Override
public Fragment getItem(int position) {
Log.v("TitleAdapter - getItem=", String.valueOf(position));
return frags[position];
}
#Override
public int getCount() {
return frags.length;
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v4.view.PagerTabStrip
android:id="#+id/pagerTabStrip"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#cccccc"/>
</android.support.v4.view.ViewPager>
</LinearLayout>
This is the image of the app working so far.
I hope you can help me.
EDIT
Following the guidance of NikolaDespotoski, decided the issue only changing the extension class for ActionBarActivity!
Actually I was thinking it had to implement the two things together, without realizing that ActionBarActivity already encompassed both.
My code looked like this, and it worked:
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
public class MainActivity extends ActionBarActivity {
// code here
}
Thanks!

Categories

Resources