0

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

kathir
  • 489
  • 2
  • 11
  • very clear from `Caused by: java.lang.ClassNotFoundException: com.example.movies.swipe.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.movies.swipe-2.apk]` so check this class in your package. – Praveen Sharma Dec 12 '13 at 04:35
  • @praveenSharma i checked , by clicking that logcat also it not showing where the error is. – kathir Dec 12 '13 at 04:42

1 Answers1

0

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

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • @GrlsHu I did , but as like before its closing and show the same error dude . – kathir Dec 12 '13 at 04:48
  • Yes changed the return count to 3 as you said in code. logcat also showing the same error. – kathir Dec 12 '13 at 04:53
  • Try to clean you project and then run. – GrIsHu Dec 12 '13 at 04:55
  • now i cleaned my project and tried. Same error dude, dunno wats the problem,. i tried with a manual , nothing wrong in that. – kathir Dec 12 '13 at 04:59
  • Have you created another two Fragments `Tamil();` and `Hindi();` ? – GrIsHu Dec 12 '13 at 05:02
  • I have checked your code and it works like charm as i did the changes which i have mentioned to you. – GrIsHu Dec 12 '13 at 05:04
  • Yes like i mentioned english i createde to show just differents colors. nothing change in that . – kathir Dec 12 '13 at 05:10
  • As i said i have tried your code and it works like charm on my side. – GrIsHu Dec 12 '13 at 05:20
  • i uploaded it in my gdrive, just check it out. https://drive.google.com/folderview?id=0B3JW1AxzgwAuY3hKaHZjWUlwN28&usp=sharing – kathir Dec 12 '13 at 05:26
  • your comments are so helpful, just now checked your blog too got useful info like speed up emulator like that. i'm new so other coding are like flying to my eyes and showing how much is there in android. – kathir Dec 12 '13 at 05:30
  • Yes , just now did that changes even i got that same error . https://drive.google.com/folderview?id=0B3JW1AxzgwAuTWY2dlJzaE9zNDg&usp=sharing – kathir Dec 12 '13 at 05:43
  • Dunno , wats problm in my side. – kathir Dec 12 '13 at 05:44
  • Try out with my code which i have posted in my answer. As i am unable to download your whole code i can not test it and check out on my side. – GrIsHu Dec 12 '13 at 05:46
  • Let me know if any problem . – GrIsHu Dec 12 '13 at 06:30
  • same prblm yar , dunno prblm with my side or with code. you got output, for that same code i'm not geting. – kathir Dec 12 '13 at 06:49
  • Download the code from here https://drive.google.com/file/d/0B7wpUaBw9SNXT1JZeS1xZHBPR00/edit?usp=sharing which i have implemented and its working great and check out. @kathir – GrIsHu Dec 12 '13 at 06:52
  • change the permisions to that, i sent request. – kathir Dec 12 '13 at 06:54
  • Done. Now check https://drive.google.com/file/d/0B7wpUaBw9SNXT1JZeS1xZHBPR00/edit?usp=sharing – GrIsHu Dec 12 '13 at 06:55
  • Yes, i got that and its working. But dunno wats wrong with mine. @GrlsHu – kathir Dec 12 '13 at 07:09
  • Now you can compare your code and check out whats wrong. :) Enjoy. – GrIsHu Dec 12 '13 at 07:13
  • Is it possible to add list in that tabs ? – kathir Dec 12 '13 at 12:42
  • Like if i create a list of movie names in each tabs and by clicking tat name it 'll intent a activity like tat , is it possible . coz i tried by extending activity to classes but in this we 'r extending fragment to do , tats y i confused. - @ GrlsHu – kathir Dec 12 '13 at 12:48