2016-03-16 71 views
1

目前有2個片段,我想補充一個,現在我花了幾個小時試圖找出如何添加它很失落,這裏是代碼,我在MainActivity的。安卓卡添加額外的選項卡,分段

public class SalaatTimesActivity extends AppCompatActivity implements Constants, 



    } 
} 

該標籤出現,但我很快點擊第2或第3個標籤崩潰。

03-16 21:27:46.203 32749-32749/com.alimuzaffar.ramadanalarm E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.alimuzaffar.ramadanalarm, PID: 32749 
    java.lang.NullPointerException 
      at com.alimuzaffar.ramadanalarm.widget.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:122) 
      at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870) 
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1086) 
      at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552) 
      at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514) 
      at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495) 
      at com.alimuzaffar.ramadanalarm.widget.SlidingTabLayout$TabClickListener.onClick(SlidingTabLayout.java:328) 
      at android.view.View.performClick(View.java:4633) 
      at android.view.View$PerformClick.run(View.java:19330) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:157) 
      at android.app.ActivityThread.main(ActivityThread.java:5356) 
      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:1265) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
      at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 
      at dalvik.system.NativeStart.main(Native Method) 

這裏是線FragmentStatePagerAdapter.java:127)

fragment.setMenuVisibility(false); 
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { 
      fragment.setUserVisibleHint(false); 
     } 
     mFragments.set(position, fragment); 
     mCurTransaction.add(container.getId(), fragment); 

     return fragment; 
    } 

frag2.class

 import android.app.Fragment; 
import android.hardware.SensorManager; 
import android.location.Location; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 


public class fragtwo extends Fragment { 

    Location mLastLocation; 
    MapFragment mMapFragment; 
    GoogleMap mMap; 

    // private float currentDegree = 0f; 
    private SensorManager mSensorManager; 

    private float[] mRotationMatrix = new float[16]; 
    private float[] mValues = new float[3]; 

    private boolean mRegistered = false; 
    private static boolean sWriterExternalPermissionDenied; 

    public fragtwo() { 
    // Required empty public constructor 
    } 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 

    } 
    } 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_two, container, false); 


    return view; 
    } 
} 

回答

0

你爲什麼要添加第三個標籤?從你的適配器代碼說,你沒有一個項目,如果position == 2:這是造成一個空項目,因此viewpager可能會崩潰,因爲它試圖實例化一個空片段。

@Override 
public Fragment getItem(int position) { 
    switch (position) { 
    case 0: 

    case 1: 
     return mKaabaLocatorFragment = KaabaLocatorFragment.newInstance(mLastLocation); 
    case 2: 
    } 
    return null; 
} 

添加return new fragtwo();情況2,你應該設置。

+0

fire3galaxy嗨啊我本來只是爲了測試,如果它崩潰了,我增加以下2種情況:從案例1行:它改變片段,但它不是將其變爲我要的fragment.class,在案例2:如何打開一個片段類 –

+0

我創建了一個新的片段類和佈局,我想從案例2調用新的佈局; –

+0

你想打開哪個片段?第一個,第二個和第三個標籤可能都是一樣的,對嗎? – Fire3galaxy