问题
StackTrace:
08-19 08:47:02.189: E/AndroidRuntime(1857): FATAL EXCEPTION: main
08-19 08:47:02.189: E/AndroidRuntime(1857): Process: com.loco.android, PID: 1857
08-19 08:47:02.189: E/AndroidRuntime(1857): java.lang.ArrayIndexOutOfBoundsException: length=1; index=4
08-19 08:47:02.189: E/AndroidRuntime(1857): at com.sit.loco.frgment.VideoListFragment.onCreateView(VideoListFragment.java:117)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:490)
08-19 08:47:02.189: E/AndroidRuntime(1857): at com.sit.loco.activity.HomeActivity$2.onTabSelected(HomeActivity.java:117)
08-19 08:47:02.189: E/AndroidRuntime(1857): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
08-19 08:47:02.189: E/AndroidRuntime(1857): at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1105)
08-19 08:47:02.189: E/AndroidRuntime(1857): at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.view.View.performClick(View.java:4438)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.view.View$PerformClick.run(View.java:18422)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.os.Handler.handleCallback(Handler.java:733)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.os.Handler.dispatchMessage(Handler.java:95)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.os.Looper.loop(Looper.java:136)
08-19 08:47:02.189: E/AndroidRuntime(1857): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-19 08:47:02.189: E/AndroidRuntime(1857): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 08:47:02.189: E/AndroidRuntime(1857): at java.lang.reflect.Method.invoke(Method.java:515)
08-19 08:47:02.189: E/AndroidRuntime(1857): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-19 08:47:02.189: E/AndroidRuntime(1857): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-19 08:47:02.189: E/AndroidRuntime(1857): at dalvik.system.NativeStart.main(Native Method)
I am getting an ArrayIndexOutOFBoundException at runtime.I didn't know how to solve this error.
Error occurred at this line YOUTUBE_USERNAME = ChannelUsername[position];
I mentioned that line in that VideoListFragment class.
VideoListFragment.java:
public class VideoListFragment extends Fragment {
OnVideoSelectedListener mCallback;
boolean loadingMore = false;
// create string variables
String YOUTUBE_USERNAME = "";
private String YOUTUBE_API = "http://gdata.youtube.com/feeds/api/users/"+YOUTUBE_USERNAME+"/uploads?alt=jsonc" +
"&v=2"+
"&start-index=1"+
"&max-results=10";
// create object of views
ListView list;
ProgressBar prgLoading;
Button btnRefresh;
Button btnLoadMore;
// create variable to get position, connection status, resources, and channel username
int position;
boolean isConnect = true;
Resources res;
String[] ChannelUsername;
// create object of custom adapter
VideoListAdapter vla;
// create arraylist variables
ArrayList<HashMap<String, String>> menuItems;
ProgressDialog pDialog;
// flag for current page
int current_page = 1;
int previous_page;
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_THUMBNAIL = "thumbnail";
static final String KEY_DURATION = "duration";
// create interface listener
public interface OnVideoSelectedListener{
public void onVideoSelected(String ID);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_list, container, false);
// connect view objects and view id on xml
list = (ListView) v.findViewById(R.id.list);
btnRefresh = (Button) v.findViewById(R.id.btnRefresh);
menuItems = new ArrayList<HashMap<String, String>>();
// get value that passed from previous page
Bundle bundle = this.getArguments();
position = bundle.getInt("position", 0);
// get channel username and put selected channel to youtube api
res = getActivity().getResources();
ChannelUsername = res.getStringArray(R.array.channel_username);
YOUTUBE_USERNAME = ChannelUsername[position];----->Error occurred at line
YOUTUBE_API = "http://gdata.youtube.com/feeds/api/users/"+YOUTUBE_USERNAME+"/uploads?alt=jsonc" +
"&v=2"+
"&start-index=1"+
"&max-results=10";
// create LoadMore button
btnLoadMore = new Button(getActivity());
btnLoadMore.setBackgroundResource(R.drawable.btn_default_holo_light);
btnLoadMore.setText(getString(R.string.load_more));
// adding load more button to lisview at bottom
list.addFooterView(btnLoadMore);
new loadFirstListView().execute();
// listener to handle load more buttton when clicked
btnLoadMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Starting a new async task
isConnect = true;
// new loadMoreListView().execute();
}
});
// listener to handle list when clicked
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
HashMap<String, String> item = new HashMap<String, String>();
item = menuItems.get(position);
mCallback.onVideoSelected(item.get("id"));
list.setItemChecked(position, true);
}
});
// listener to handle refresh button when clicked
btnRefresh.setOnClickListener(new OnClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isConnect = true;
new loadFirstListView().execute();
}
});
return v;
}
}
strings.xml:
<string-array name="channel_name">
<item >The Father House</item>
</string-array>
<string-array name="channel_username">
<item >TheJesudian</item>
</string-array>
Anybody can help me with this.Thank you.
回答1:
You are trying to get a position that array doesn't have. The int of the position is bigger than ChannelUsername size.
来源:https://stackoverflow.com/questions/25384004/arrayindexoutofboundexception