目前有2个片段,我想现在添加一个,我花了几个小时试图找出如何添加它丢失,这是我的主要活动的代码。
public class SalaatTimesActivity extends AppCompatActivity implements Constants,
}
}
标签显示但是当我点击第二或第三个标签时它会崩溃。
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 :(得分:0)
为什么要添加第3个标签?根据你的适配器代码所说的,如果position == 2
你没有项目:这会导致给出一个空项目,因此viewpager可能会崩溃,因为它试图实例化一个null片段。
@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,您应该设置。