-1

My MainActivity:

 public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
    private ViewPager viewPager;
    private ActionBar actionBar;
    private TabsFragmentPagerAdapter tabsAdapter;
    private String[] days = new String[]{"Monday","Tuesday"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabsAdapter = new TabsFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(tabsAdapter);
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for(int i=0; i<3; i++){
            actionBar.addTab(actionBar.newTab().setText(days[i]).setTabListener(this));
        }
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg) {
                // TODO Auto-generated method stub
                actionBar.setSelectedNavigationItem(arg);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

    @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_main, menu);
        return true;
    }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
}

My manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.muhammad_taha.tablelayout" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And my next code:

 public class MondayFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View view = inflater.inflate(R.layout.fragment_monday, container, false);
            return view;
        }

    }

And next one:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TuesdayFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_tuesday, container, false);
        return view;
    }

}

And for stack trace sort of:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsFragmentPagerAdapter extends FragmentPagerAdapter {

    public TabsFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int index) {
        // TODO Auto-generated method stub
        if(index == 0)
            return new MondayFragment();
        if(index == 1)
            return new TuesdayFragment();


        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

}

My xml files to show first page:

<?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:background="#00fff8" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Monday"
        android:textSize="40dp"
        android:gravity="center" />

</LinearLayout>

My xml file to show second tab:

<?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:background="#59ff85" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Tuesday"
        android:textSize="40dp"
        android:gravity="center" />

</LinearLayout>

Well at the end I want to tell you I am stuck on that from 3 days. Please correct this code. The "TabListener" interface in not available. But by ignoring it and run my app after few milli sec and then bhoooo "Unfortunately your application has stop working"............ please help me..... and write correct code for me.......... now brain is finish.........and I am finish. I really have no idea what's going on backend. I want help to solve that problem. If I know whats is the problem I might solved it myself. I need help to identify problems + if you know solution. And If you need some other files tell me i'll load it. But help me.

  • 4
    Hi, its really difficult to get some help here if you post a question like this... UPPERCASE its like shouting, and doesnt explain well your problem... – melli-182 Jul 10 '15 at 18:58
  • um sorry...... about that – Muhammad Taha Jul 10 '15 at 19:00
  • I am new. sorry..... – Muhammad Taha Jul 10 '15 at 19:06
  • 1
    As everybody said, don't use _caps lock_ it looks like you're shouting and you need to post your real problem, not just copy and paste your code and expect someone corret. In this case, your stacktrace and the code related to the problem will be more useful. So please, improve your question to let us help you. – Álisson Morais Jul 10 '15 at 20:08
  • Sir @Alisson Morais. Thanks for comment. And sorry about uppercase thing.I am new on stack. I don't know where is the problem! my code compile perfectly, build perfectly but didn't work on amulator.. and can you please tell me where did I find that stacktrace thing in android studio? – Muhammad Taha Jul 11 '15 at 09:20
  • Hello @MuhammadTaha, you can get the stacktrace by opening the LogCat feature. Hit _Alt + 6_ and the window will be opened, then run your application and see the full log output. – Álisson Morais Jul 11 '15 at 14:15

1 Answers1

1

your getCount implementation is wrong. getItem is 0-based, and when position is 2, it returns null, but it can't return null. Change

@Override
public int getCount() {
    return 3;
}

to

@Override
public int getCount() {
    return 2;
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305