0

ViewPagerFragment.java

package nsixty.crew.app;

import java.util.List;
import java.util.Vector;



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.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class ViewPagerFragment extends FragmentActivity {

    private PagerAdapter mPagerAdapter;

    ViewPager pager;

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

        this.initialisePaging();
    }

    private void initialisePaging() {

        List<Fragment> fragments = new Vector<Fragment>();

        fragments
                .add(Fragment.instantiate(this, LoginActivity.class.getName()));
        fragments.add(Fragment.instantiate(this, send.class.getName()));
        this.mPagerAdapter = new PagerAdapter(
                super.getSupportFragmentManager(), fragments);

        pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(this.mPagerAdapter);

    }

    public class PagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public int getCount() {
            return this.fragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

    }

}

lcmeter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+android:id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Androidmanifest.xml

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

    <support-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="false" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:icon="@drawable/icon87"
        android:label="@string/app_name" >
        <activity android:name="ViewPagerFragment" >

            <!--
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Dialog"
            -->

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".main" >
            <intent-filter>
                <action android:name="nsixty.crew.app.main" />

                <category
                    android:name="android.intent.category.DEFAULT"
                    android:screenOrientation="portrait" />
            </intent-filter>
        </activity>
        <activity
            android:name=".play"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="nsixty.crew.app.play" />

                <category
                    android:name="android.intent.category.DEFAULT"
                    android:screenOrientation="landscape" />
            </intent-filter>
        </activity>
        <activity
            android:name=".secondView"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="nsixty.crew.app.SECONDVIEW" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".recorder"
            android:configChanges="orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="nsixty.crew.app.RECORD" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".send"
            android:label="@string/app_name" >

            <!-- android:theme="@android:style/Theme.Dialog" -->
            <intent-filter>
                <action android:name="nsixty.crew.app.SHARE" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".gallery"
            android:theme="@android:style/Theme.Dialog" >
            <intent-filter>
                <action android:name="nsixty.crew.app.gallery" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".RegisterActivity"
            android:theme="@android:style/Theme.Dialog" >
        </activity>
        <activity
            android:name=".media"
            android:theme="@android:style/Theme.Dialog" >
            <intent-filter>
                <action android:name="nsixty.crew.app.media" />

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

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

    <uses-permission android:name="android.permission.WRITE_SETTINGS" >
    </uses-permission>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <uses-feature android:name="android.hardware.camera" />

</manifest>

日志猫

08-28 11:28:34.149: E/AndroidRuntime(18946): FATAL EXCEPTION: main
08-28 11:28:34.149: E/AndroidRuntime(18946): java.lang.RuntimeException: Unable to start activity ComponentInfo{nsixty.crew.app/nsixty.crew.app.ViewPagerFragment}: java.lang.ClassCastException: nsixty.crew.app.LoginActivity
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.os.Looper.loop(Looper.java:130)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.app.ActivityThread.main(ActivityThread.java:3703)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at java.lang.reflect.Method.invokeNative(Native Method)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at java.lang.reflect.Method.invoke(Method.java:507)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at dalvik.system.NativeStart.main(Native Method)
08-28 11:28:34.149: E/AndroidRuntime(18946): Caused by: java.lang.ClassCastException: nsixty.crew.app.LoginActivity
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at nsixty.crew.app.ViewPagerFragment.initialisePaging(ViewPagerFragment.java:35)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at nsixty.crew.app.ViewPagerFragment.onCreate(ViewPagerFragment.java:28)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-28 11:28:34.149: E/AndroidRuntime(18946):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
08-28 11:28:34.149: E/AndroidRuntime(18946):    ... 11 more

如果我在没有 ViewPager 的情况下正常运行应用程序,我的 LoginActivity 类和发送类工作正常。所以我没有在这里添加这些类的代码。我只是想让我的应用程序更时尚这就是为什么我使用视图寻呼机谢谢

4

2 回答 2

0

我认为 LoginActivity 是扩展 Activity 类而不是 Fragment 类。这就是你得到classcastException的原因。该类需要是一个片段

于 2012-08-28T16:05:22.307 回答
0

以下是清单文件中的条目。

<activity android:name=".LoginActivity" >

您不能从 Activity 扩展 LoginActivity 并实例化它..它必须扩展 Fragment 。

于 2012-08-28T16:10:38.313 回答