Simple swipe tabs shows error at runtime in android

1.3k views Asked by At

I have placed five activity , one to view pager one for tabs activity and another three for contents in that three tabs like a dashboard.

MainActivity.java

package com.example.movies.swipe;

import android.annotation.SuppressLint;
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;
import android.view.Menu;

import com.example.movies.swipe.adapter.TabsPagerAdapter;

@SuppressWarnings("unused")
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {


    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;

    private String[] tabs = { "English", "Tamil", "Hindi" };




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        


        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }


        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {

                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });



    }

    @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;
    }

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

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

        viewPager.setCurrentItem(tab.getPosition());


    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

}

TabsPagerAdapter.java

package com.example.movies.swipe.adapter;


import com.example.movies.swipe.English;
import com.example.movies.swipe.Hindi;
import com.example.movies.swipe.Tamil;

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

public class TabsPagerAdapter extends FragmentPagerAdapter{




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

    @Override
    public Fragment getItem(int index) {


        switch (index) {
        case 0:

            return new English();
        case 1:

            return new Tamil();
        case 2:

            return new Hindi();
        }





        // TODO Auto-generated method stub
        return null;
    }

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

}

English.java , Tamil.java and Hindi.java in all three i created same code like this

package com.example.movies.swipe;

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


@SuppressWarnings("unused")
public class English extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.english, container, false);

        return rootView;
    }



}

activity_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

english.xml , tamil.xml and hindi.xml all are alike.

<?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:orientation="vertical"
    android:background="#fa6a6a" >

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="English"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>


</RelativeLayout>

My Android_Manifest.xml file is

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.movies.swipe"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.movies.swipe.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>
        <activity android:name="com.example.movies.swipe.English" />
        <activity android:name="com.example.movies.swipe.Tamil" />
        <activity android:name="com.example.movies.swipe.Hindi" />
        <activity android:name="com.example.movies.swipe.adapter.TabsPagerAdapter" />
    </application>

</manifest>

Nothing show error in my project but while running in logcat it show some error . And the application shows a black screen and anked for force close alert.

Logcat

12-12 09:55:06.986: E/AndroidRuntime(329): FATAL EXCEPTION: main
12-12 09:55:06.986: E/AndroidRuntime(329): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.movies.swipe/com.example.movies.swipe.MainActivity}: java.lang.ClassNotFoundException: com.example.movies.swipe.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.movies.swipe-2.apk]
12-12 09:55:06.986: E/AndroidRuntime(329):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-12 09:55:06.986: E/AndroidRuntime(329):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-12 09:55:06.986: E/AndroidRuntime(329):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-12 09:55:06.986: E/AndroidRuntime(329):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-12 09:55:06.986: E/AndroidRuntime(329):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 09:55:06.986: E/AndroidRuntime(329):  at android.os.Looper.loop(Looper.java:123)
12-12 09:55:06.986: E/AndroidRuntime(329):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-12 09:55:06.986: E/AndroidRuntime(329):  at java.lang.reflect.Method.invokeNative(Native Method)
12-12 09:55:06.986: E/AndroidRuntime(329):  at java.lang.reflect.Method.invoke(Method.java:507)
12-12 09:55:06.986: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-12 09:55:06.986: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-12 09:55:06.986: E/AndroidRuntime(329):  at dalvik.system.NativeStart.main(Native Method)
12-12 09:55:06.986: E/AndroidRuntime(329): Caused by: java.lang.ClassNotFoundException: com.example.movies.swipe.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.movies.swipe-2.apk]
12-12 09:55:06.986: E/AndroidRuntime(329):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
12-12 09:55:06.986: E/AndroidRuntime(329):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
12-12 09:55:06.986: E/AndroidRuntime(329):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-12 09:55:06.986: E/AndroidRuntime(329):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-12 09:55:06.986: E/AndroidRuntime(329):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
12-12 09:55:06.986: E/AndroidRuntime(329):  ... 11 more
12-12 09:55:11.156: I/Process(329): Sending signal. PID: 329 SIG: 9

Emulator Error

1

There are 1 answers

22
GrIsHu On BEST ANSWER

In your TabsPagerAdapter you need to return the total number of Fragments in your getCount method not 0. Change as below in your method.

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

Also in your Manifest file you do not need to register your Fragment and TabsPagerAdapter classes as they are the Fragment. Just remove the below lines from your Manifest file.

    <activity android:name="com.example.movies.swipe.English" />
    <activity android:name="com.example.movies.swipe.Tamil" />
    <activity android:name="com.example.movies.swipe.Hindi" />
    <activity android:name="com.example.movies.swipe.adapter.TabsPagerAdapter" />

EDITED:

Try out with below code which i have tried and it works like charm on my side. No changes in the layout file.

MainActivity.java

@SuppressWarnings("unused")
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;

    private String[] tabs = { "English", "Tamil", "Hindi" };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {

            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

}

@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;
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub
}

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

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

}

In adapter class right now i have just loaded English fragment just for testing.

TabsPagerAdapter.java

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

public class TabsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:

            return new English();
        case 1:

            return new English();
        case 2:

            return new English();
        }
        return null;
    }

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

}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tabs.abc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.tabs.abc.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>

Output:

enter image description here

enter image description here