Ajinkya More
Ajinkya More

Reputation: 425

App force close when changing items in StringArray, Android?

right now I'm working on a app which changes items in StringArray on button clicks. I've implemented left and right buttons, when I click right button the item changes by right, when I press left the item changes to left. Up to here everything is fine, but when I click left button when the item is on Steve Jobs the app force closes. Any idea why this is happening?

Strings.xml:

<string name="sj">Steve Jobs</string>
<string name="mz">Mark Zuckerberg</string>
<string name="bg">Bill Gates</string>
<string name="lp">Larry Page</string>

<string-array name="gl">
    <item>@string/sj</item>
    <item>@string/mz</item>
    <item>@string/bg</item>
    <item>@string/lp</item>
</string-array>

MainActivity.java:

package com.example.theatremode;

import android.support.v7.app.ActionBarActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements OnClickListener {

int counter = 0;
TextView tv1;
Button button1;
Button button2;

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

    tv1 = (TextView) findViewById(R.id.tv1);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);

    Resources res = getResources();
    final String[] list = res.getStringArray(R.array.gl); // get the array
    ((TextView) findViewById(R.id.tv1)).setText(list[counter]); // set the
                                                                // initial
                                                                // message.

    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView tx = (TextView) findViewById(R.id.tv1);
            counter++;
            if (counter >= list.length)
                counter = 0;

            tx.setText(list[counter]); // set the new message.

        }
    });

    Resources res2 = getResources();
    final String[] list2 = res2.getStringArray(R.array.gl); // get the array
    ((TextView) findViewById(R.id.tv1)).setText(list2[counter]);

    button2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView tx2 = (TextView) findViewById(R.id.tv1);
            counter--;
            if (counter >= list2.length)
                counter = 0;

            tx2.setText(list2[counter]); // set the new message.

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}

}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.theatremode.MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:id="@+id/tv1"/>

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignLeft="@+id/tv1"
    android:text="Left" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="48dp"
    android:layout_marginRight="14dp"
    android:text="Right" />

Upvotes: 2

Views: 58

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

Looks like you have a IndexOutOfBoundsException.

It's not obvious exactly what's going on here, but this might fix it:

 button2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView tx2 = (TextView) findViewById(R.id.tv1);
            counter--;
            //if (counter >= list2.length)
                //counter = 0;
            if (counter < 0)
                counter = list2.length - 1;

            tx2.setText(list2[counter]); // set the new message.

        }
    });

Upvotes: 2

Related Questions