我无法访问我的应用中的最后一个片段(案例7)。我一直在调试它,我认为这是一个逻辑错误,但我很难解决它。
中的部分public void onSectionAttached(int number) {
String[] stringArray = getResources().getStringArray(R.array.section_titles);
if (number >= 1) {
mTitle = stringArray[number-1];
}
}
所以这是我的代码从android studio中的默认导航抽屉派生的部分:
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
switch(position+1){
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, onClickReserveSeat.newInstance(position + 1)).addToBackStack("tag")
.commit();
break;
case 2:
fragmentManager.beginTransaction()
.replace(R.id.container, onClickCancelSeat.newInstance(position + 1)).addToBackStack("tag")
.commit();
break;
case 3:
fragmentManager.beginTransaction()
.replace(R.id.container, onClickMyTrips.newInstance(position + 1)).addToBackStack("tag")
.commit();
break;
case 4:
fragmentManager.beginTransaction()
.replace(R.id.container, onClickUserAccount.newInstance(position + 1)).addToBackStack("tag")
.commit();
break;
case 5:
fragmentManager.beginTransaction()
.replace(R.id.container, onClickLogout.newInstance(position + 1)).addToBackStack("tag")
.commit();
break;
case 6:
fragmentManager.beginTransaction()
.replace(R.id.container, onClickHelp.newInstance(position + 1)).addToBackStack("tag")
.commit();
break;
case 7:
fragmentManager.beginTransaction()
.replace(R.id.container, onClickAboutUs.newInstance(position + 1)).addToBackStack("tag")
.commit();
break;
}
}
public void onSectionAttached(int number) {
String[] stringArray = getResources().getStringArray(R.array.section_titles);
if (number >= 1) {
mTitle = stringArray[number-1];
}
}
onClickAboutUs(最后片段类/方法)
public class onClickAboutUs extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static onClickAboutUs newInstance(int sectionNumber) {
onClickAboutUs fragment = new onClickAboutUs();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public onClickAboutUs() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about_us, container,
false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((UserAccount) activity).onSectionAttached(getArguments().getInt(
ARG_SECTION_NUMBER));
}
}
编辑:(logcat)
java.lang.ArrayIndexOutOfBoundsException: length=6; index=6
at com.oop.admin.reserbus.UserAccount.onSectionAttached(UserAccount.java:120)
at com.oop.admin.reserbus.onClickAboutUs.onAttach(onClickAboutUs.java:37)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
你能试试吗
int pos = position+1;
switch(pos){
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, onClickReserveSeat.newInstance(position + 1)).addToBackStack("tag")
.commit();
break;
...
}