user3222452
user3222452

Reputation: 1

Array-OutOfBound Exception in android

i'm new to android and got stuck up in horizontal swipe. let me explain in detail, i have couple of datas in ListView if the user clicks the ListView item- Detailed description page gets opened, here i need to swipe the "Detailed Description" page horizontally. i downloaded and tested some of the tutorials using pageIndicator library. finally was able to get ListView data but when clicked on list item it gets force close, the error in logcat says OutOfBound Exception Please help me, thanks.

Logcat Exception

01-22 12:28:46.530: E/AndroidRuntime(1084): FATAL EXCEPTION: main
01-22 12:28:46.530: E/AndroidRuntime(1084): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-22 12:28:46.530: E/AndroidRuntime(1084):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
01-22 12:28:46.530: E/AndroidRuntime(1084):     at java.util.ArrayList.get(ArrayList.java:311)
01-22 12:28:46.530: E/AndroidRuntime(1084):     at com.jokes.LayoutTwo.onCreateView(LayoutTwo.java:70)
 01-22 12:28:46.530: E/AndroidRuntime(1084):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-22 12:28:46.530: E/AndroidRuntime(1084):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)

LayoutTwo.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.samples, null);

         TextView txttitle = (TextView) root.findViewById(R.id.titles);
  ///////// The exception arises in below line ///////
     txttitle.setText(Common.rowItems.get(Common.itemnumber).get("strtitle"));   

     TextView txtDescription = (TextView) root.findViewById(R.id.description);
     txtDescription.setText(Common.rowItems.get(Common.itemnumber).get("strDesc"));

     Intent in = getActivity().getIntent();        
        String strtitle = in.getStringExtra("strtitle");
        String strDesc = in.getStringExtra("strDesc");

    return root;
}

But i have given the array length correctly in MainActivity. MainActivity.java

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

 rowItems = new ArrayList<RowItem>();
 for (int i = 0; i < titles.length; i++) {
 RowItem item = new RowItem(titles[i], descriptions[i]);
 rowItems.add(item);
  }

  listView = (ListView) findViewById(R.id.list);
 CustomListViewAdapter adapter = new CustomListViewAdapter(this,
    R.layout.list_item, rowItems);
  listView.setAdapter(adapter);
  listView.setOnItemClickListener(this);
   }

 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position,
  long id) {
TextView txttl = (TextView) view.findViewById(R.id.title);  
String strtitle = txttl.getText().toString();


TextView txtls = (TextView) view.findViewById(R.id.desc);   
String strDesc = txtls.getText().toString();

// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), ViewPagerStyle1Activity.class);
// sending data to new activity
i.putExtra("strtitle", strtitle);
i.putExtra("strDesc", strDesc);
startActivity(i);
   }
 }

Upvotes: 0

Views: 325

Answers (2)

Fildor
Fildor

Reputation: 16104

txttitle.setText(Common.rowItems.get(Common.itemnumber).get("strtitle"));

I guess Common.rowItems has not been filled by the time this line is called. So it is empty. Now Common.itemnumber seems to be "0". Which asks an empty structure for its first element.

To solve this, you'll have to check if Common.itemnumber has a reasonable value to index into Common.rowItems first. And if not, then set a default text or something like that. Or of course, make sure Common.rowItems has been filled. But you still should verify the index is not out of bounds.

Upvotes: 1

Ravind Maurya
Ravind Maurya

Reputation: 977

use like this :

Intent in = getActivity().getIntent();        
String strtitle = in.getStringExtra("strtitle");
String strDesc = in.getStringExtra("strDesc");

TextView txttitle = (TextView) root.findViewById(R.id.titles);
TextView txtDescription = (TextView) root.findViewById(R.id.description);

txttitle.setText(strtitle);        
txtDescription.setText(strDesc);

Upvotes: 0

Related Questions