1

在我更改此代码后:

 @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

对此:

    @Override
    public Fragment getItem(int index) {

        Fragment fragment = null;
        switch(index){
        case 0:
             fragment = new DummySectionFragment();
             break;
        case 1:
             fragment = new Fragment2();
        default:
            break;
        }

        //return fragment
        return fragment;
    }

我的应用程序出现了一系列错误,导致它无法启动。谁能告诉我第二个代码中可能有什么问题?我只是在寻找一种简单设置片段的方法。

控制台的日志:

[2013-08-20 22:11:53 - Android SDK] Warning when loading the SDK:

Warning: Ignoring platform 'android-18': build.prop is missing.
[2013-08-20 22:12:50 - Testare5] ------------------------------
[2013-08-20 22:12:50 - Testare5] Android Launch!
[2013-08-20 22:12:50 - Testare5] adb is running normally.
[2013-08-20 22:12:51 - Testare5] Performing com.example.testare5.MainActivity activity launch
[2013-08-20 22:12:51 - Testare5] Automatic Target Mode: launching new emulator with compatible AVD 'Nexin'
[2013-08-20 22:12:51 - Testare5] Launching a new emulator with Virtual Device 'Nexin'
[2013-08-20 22:13:11 - Testare5] New emulator found: emulator-5554
[2013-08-20 22:13:11 - Testare5] Waiting for HOME ('android.process.acore') to be launched...
[2013-08-20 22:15:54 - Testare5] HOME is up on device 'emulator-5554'
[2013-08-20 22:15:54 - Testare5] Uploading Testare5.apk onto device 'emulator-5554'
[2013-08-20 22:15:59 - Testare5] Installing Testare5.apk...
[2013-08-20 22:18:16 - Testare5] Failed to install Testare5.apk on device 'emulator-5554!
[2013-08-20 22:18:16 - Testare5] (null)
[2013-08-20 22:18:17 - Testare5] Launch canceled!

这是 LogCat的一瞥:(打印链接http://s8.postimg.org/wosscd9z9/Log_Cat2.jpg,http://s10.postimg.org/9ur8tmwo9/Log_Cat.jpg

08-20 19:45:58.898: E/AndroidRuntime(913):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.View.measure(View.java:15518)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.View.measure(View.java:15518)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.View.measure(View.java:15518)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-20 19:45:58.898: E/AndroidRuntime(913):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.View.measure(View.java:15518)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.Choreographer.doFrame(Choreographer.java:532)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.os.Handler.handleCallback(Handler.java:725)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.os.Looper.loop(Looper.java:137)
08-20 19:45:58.898: E/AndroidRuntime(913):  at android.app.ActivityThread.main(ActivityThread.java:5041)
08-20 19:45:58.898: E/AndroidRuntime(913):  at java.lang.reflect.Method.invokeNative(Native Method)
08-20 19:45:58.898: E/AndroidRuntime(913):  at java.lang.reflect.Method.invoke(Method.java:511)
08-20 19:45:58.898: E/AndroidRuntime(913):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-20 19:45:58.898: E/AndroidRuntime(913):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-20 19:45:58.898: E/AndroidRuntime(913):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

确保getItem()永远不会返回null

@Override
public Fragment getItem(int index) {
    Fragment fragment;
    switch(index) {
        case 0:
            fragment = new DummySectionFragment();
            break;
        case 1:
        default: 
            fragment = new Fragment2();
            break;
    }
    return fragment;
}
于 2013-08-21T05:28:40.853 回答