2014-12-06 95 views
0

我嘗試在仿真器打開我的Android應用程序的工作室,它是給了一個錯誤:這是什麼意思?我如何解決它?致命異常:主要

12-06 00:50:30.993 1198-1198/com.coding.nero E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.coding.nero, PID: 1198 
java.lang.NullPointerException 
     at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394) 
     at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389) 
     at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99) 
     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832) 
     at android.support.v4.view.ViewPager.populate(ViewPager.java:982) 
     at android.support.v4.view.ViewPager.populate(ViewPager.java:914) 
     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) 
     at android.view.View.measure(View.java:16497) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
     at android.view.View.measure(View.java:16497) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327) 
     at android.view.View.measure(View.java:16497) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) 
     at android.view.View.measure(View.java:16497) 
     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916) 
     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113) 
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295) 
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670) 
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
     at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
     at android.view.Choreographer.doFrame(Choreographer.java:544) 
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
     at android.os.Handler.handleCallback(Handler.java:733) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5017) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
     at dalvik.system.NativeStart.main(Native Method) 

我不知道這意味着什麼或如何解決它。如有必要,我可以提供更多代碼。當我在模擬器中打開它時,它說該應用程序崩潰並關閉。

這是我的主要活動XML文件

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

這裏是我的MainActivity的Java

package com.coding.nero; 

import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class MainActivity extends FragmentActivity { 

    ViewPager viewPager=null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     viewPager= (ViewPager) findViewById(R.id.pager); 
     FragmentManager fragmentManager=getSupportFragmentManager(); 
     viewPager.setAdapter(new MyAdapter(fragmentManager)); 
    } 


    /** 
    * Created by John on 12/5/14. 
    */ 

    //Links class to layout file fragment_a 
    public static class FragmentA extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_a, container, false); 
     } 
    } 

    /** 
    * Created by John on 12/5/14. 
    */ 

    //Links class to layout file fragment_a 
    public static class FragmentB extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_a, container, false); 
     } 
    } 

    /** 
    * Created by John on 12/5/14. 
    */ 

    //Links class to layout file fragment_a 
    public static class FragmentC extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_a, container, false); 
     } 
    } 

    /** 
    * Created by John on 12/5/14. 
    */ 

    //Links class to layout file fragment_a 
    public static class FragmentD extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_d, container, false); 
     } 
    } 
} 

class MyAdapter extends FragmentPagerAdapter{ 

    public MyAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int i) { 
     Fragment fragment=null; 
     if(i==0){ 
      fragment=new FragmentA_1(); 
     } 
     if(i==1){ 
      fragment=new FragmentB_1(); 
     } 
     if(i==2){ 
      fragment=new FragmentC_1(); 
     } 
     if(i==3){ 
      fragment=new FragmentD_1(); 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     return 4; 
    } 

適配器代碼:

package com.coding.nero; 

import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class MainActivity extends FragmentActivity { 

    ViewPager viewPager=null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     viewPager= (ViewPager) findViewById(R.id.pager); 
     FragmentManager fragmentManager=getSupportFragmentManager(); 
     viewPager.setAdapter(new MyAdapter(fragmentManager)); 
    } 


    /** 
    * Created by John on 12/5/14. 
    */ 

    //Links class to layout file fragment_a 
    public static class FragmentA extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_a, container, false); 
     } 
    } 

    /** 
    * Created by John on 12/5/14. 
    */ 

    //Links class to layout file fragment_a 
    public static class FragmentB extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_a, container, false); 
     } 
    } 

    /** 
    * Created by John on 12/5/14. 
    */ 

    //Links class to layout file fragment_a 
    public static class FragmentC extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_a, container, false); 
     } 
    } 

    /** 
    * Created by John on 12/5/14. 
    */ 

    //Links class to layout file fragment_a 
    public static class FragmentD extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_d, container, false); 
     } 
    } 
} 

class MyAdapter extends FragmentPagerAdapter{ 

    public MyAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int i) { 
     Fragment fragment=null; 
     if(i==0){ 
      fragment=new FragmentA_1(); 
     } 
     if(i==1){ 
      fragment=new FragmentB_1(); 
     } 
     if(i==2){ 
      fragment=new FragmentC_1(); 
     } 
     if(i==3){ 
      fragment=new FragmentD_1(); 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     return (4); 
    } 
} 
+0

需要查看您的活動佈局xml文件 – 2014-12-06 06:06:51

+0

您可以放置​​活動代碼嗎? – Mehrdad 2014-12-06 09:48:19

+0

你是什麼意思 – user3566841 2014-12-06 16:50:55

回答

0

因爲你的適配器「的getItem( )「返回空值。

+0

我該如何解決? – user3566841 2014-12-08 04:55:40

+0

在你的「getItem()」方法中,你正在創建一個片段實例,但你永遠不會返回它,所以只要返回片段實例就可以解決這個問題。 @Override public Fragment getItem(int i){ Fragment fragment = null; // if ... statement return fragment; } – KeithYokoma 2014-12-08 04:57:09

+0

對不起剛剛意識到這是重複的 – user3566841 2014-12-08 05:06:47

相關問題