Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6667169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:54:18+00:00 2026-05-26T02:54:18+00:00

When I load a gridview (with adapter weekAdapter) from inside a fragment (weekFragment) for

  • 0

When I load a gridview (with adapter weekAdapter) from inside a fragment (weekFragment) for a viewpager, the first page loads fine. However, once I want to switch page, it gives a StringIndexOutOfBoundsException. I can’t figure out what is wrong. In the LogCat nothing is reported, and the error only shows native methods, that I don’t have modified. Please tell me how I can solve this.

weekFragment.java:

package nl.siebeh.schoolmate;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

public class weekFragment extends Fragment {
    dbLayer db;
    String[] array = new String[60];
    public String title;
    View v;

    /*@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        GridView gridview = (GridView) v.findViewById(R.id.gridview);
        gridview.setAdapter(new weekAdapter(getActivity(), mContent));
    }*/

    public weekFragment newInstance(String title) {
        weekFragment fragment = new weekFragment();
        fragment.title = title;
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        db = new dbLayer(getActivity());

        for(int i = 0; i < 60; i++){
            this.array[i] = String.valueOf(i);
        }

        ArrayList<ArrayList<Object>> result = new ArrayList<ArrayList<Object>>();

        if(title == "leraren"){
            result = db.getAllRowsAsArrays("weekTeacher", null); 
        }else if(title == "locatie"){
            result = db.getAllRowsAsArrays("weekLocation", null); 
        }else if(title == "vakken"){
            result = db.getAllRowsAsArrays("weekSubjects", null); 
        }
        Log.i("SchoolMate", "Size of result: "+String.valueOf(result.size()));
        for(int position = 0; position < result.size(); position++){
            ArrayList<Object> row = result.get(position);
            int hour = Integer.valueOf(row.get(1).toString()).intValue();
            int day =  Integer.valueOf(row.get(0).toString()).intValue();
            int pos = 6 * hour + day;
            this.array[pos] = row.get(position).toString();
        }

        this.array[2] = getResources().getStringArray(R.array.days)[0];
        this.array[3] = getResources().getStringArray(R.array.days)[1];
        this.array[4] = getResources().getStringArray(R.array.days)[2];
        this.array[5] = getResources().getStringArray(R.array.days)[3];
        this.array[6] = getResources().getStringArray(R.array.days)[4];

        for(int i = 1; i < 10; i++){
            this.array[i] = Integer.toString(i);
        }

        v = inflater.inflate(R.layout.week_fragment, container, false);
        GridView gridview = (GridView) v.findViewById(R.id.gridview);
        if(getActivity() == null){
            Log.i("SchoolMate", "getActivty() returns null");
        }
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < 60; i++){
            sb.append("array["+i+"] = \""+array[i]+"\" \n");
        }
        Log.i("SchoolMate", sb.toString());
        gridview.setAdapter(new weekAdapter(getActivity(), array));
        return gridview;
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        return super.onContextItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
    }



}

weekAdapter.java:

package nl.siebeh.schoolmate;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class weekAdapter extends BaseAdapter {
    public String[] strings;
    Context mContext;

    public weekAdapter(Context c, String[] s){
        strings = s.clone();
        mContext = c;
        Log.i("SchoolMate", "weekAdapter called");
    }

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

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv;
        //if (convertView == null) {  // if it's not recycled, initialize some attributes
            tv = new TextView(mContext);
            try{
                tv.setText(strings[position]);
            }catch(ArrayIndexOutOfBoundsException e){
                Log.i("SchoolMate", "Out of bounds; position = "+String.valueOf(position));
            }
            tv.setBackgroundColor(Color.BLACK);
            tv.setTextColor(Color.WHITE);
        /*} else {
            tv = (TextView) convertView;
        }*/

        return tv;
    }

}

The error:

ViewRoot.draw(boolean) line: 1546   
ViewRoot.performTraversals() line: 1258 
ViewRoot.handleMessage(Message) line: 1859  
ViewRoot(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 123 
ActivityThread.main(String[]) line: 3683    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 507  
ZygoteInit$MethodAndArgsCaller.run() line: 839  
ZygoteInit.main(String[]) line: 597 
NativeStart.main(String[]) line: not available [native method]  

EDIT:
I determined that the else if(title == “vakken”) is the cause. If it is commented out, it just works. The order doesn’t matter, it is this clause that causes the error. Later today I will try to make just seperate ifs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:54:18+00:00Added an answer on May 26, 2026 at 2:54 am

    Ok, I found out my problem. Here:

    }else if(title == "vakken"){
         result = db.getAllRowsAsArrays("weekSubjects", null); 
    }
    

    weekSubjects should be weekSubject (without the s at the end)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a gridview with image adapter. The adapter load images from web
I have an aspx page with a gridview. In my page load event, I
Using VS2005 I am using Gridview in my web page, when i load the
I had gridview which in load it will get data from database .And I
How to load, save and display data from GridView to XML? Not XML file
I have a gridview that is bound to a datasoure on page load. The
If I bind GridView to SqlDataSource, then the first time page will be created,
I have a gridview that I load from a linq query. MyGridData is a
I load a UIWebView from a string in a navigation based app. I'm using
ViewA load some data from Coredata, then push to the next viewB . In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.