简单的滑动选项卡在运行时在android中显示错误

时间:2013-12-12 04:28:48

标签: android runtime android-viewpager swipeview

我已经放置了五个活动,一个用于查看分页器一个用于标签活动,另外三个用于三个标签中的内容,如仪表板。

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和Hindi.java在这三个中我都创建了相同的代码

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和hindi.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: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>

我的Android_Manifest.xml文件是

<?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>

我的项目中没有显示任何错误,但在logcat中运行时显示出一些错误。该应用程序显示黑屏和强制关闭警报。

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

模拟器错误

1 个答案:

答案 0 :(得分:0)

TabsPagerAdapter中,您需要在getCount方法中返回片段总数而不是0。在您的方法中更改如下。

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

同样在您的清单文件中,您无需注册FragmentTabsPagerAdapter类,因为它们是Fragment。只需从您的清单文件中删除以下行。

    <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" />

<强>编辑:

尝试下面我尝试过的代码,它就像我身边的魅力一样。布局文件没有变化。

<强> 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
}

}

在适配器类中,我刚刚加载了English片段,仅用于测试。

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

}

<强>清单

<?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>

<强>输出:

enter image description here

enter image description here