bhanu kaushik
bhanu kaushik

Reputation: 391

Android Application crash Issue

I am new to android and working on real simple application. I've declared some strings array in String.xml and displaying those string on Text View one by one.User can change the strings on press of "Next" and "Previous" buttons.

Problem i am facing:

  1. First String is not getting displayed when application is launched
  2. When all string are finished and still next button is pressed, application crashes.

MainActivity.java

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            final String allquotes[] = getResources()
                    .getStringArray(R.array.quotes);
            counter = 0;
            display = (TextView) findViewById(R.id.tvdisplay);
            choosenext = (Button) findViewById(R.id.bnext);

            choosenext.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    counter++;
                    display.setText(allquotes[counter]);

                }
            });

            chooseprevious = (Button)findViewById(R.id.bprevious);
            chooseprevious.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                counter--;
                            display.setText(allquotes[counter]);

                }
            });


        }


    }

logcat

02-15 12:52:37.020: W/KeyCharacterMap(405): No keyboard for id 0
02-15 12:52:37.020: W/KeyCharacterMap(405): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-15 12:52:47.509: D/dalvikvm(405): GC_EXTERNAL_ALLOC freed 54K, 52% free 2586K/5379K, external 2237K/2793K, paused 382ms
02-15 12:52:59.420: D/AndroidRuntime(405): Shutting down VM
02-15 12:52:59.420: W/dalvikvm(405): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-15 12:52:59.431: E/AndroidRuntime(405): FATAL EXCEPTION: main
02-15 12:52:59.431: E/AndroidRuntime(405): java.lang.ArrayIndexOutOfBoundsException
02-15 12:52:59.431: E/AndroidRuntime(405):  at com.example.zzzz.MainActivity$1.onClick(MainActivity.java:34)
02-15 12:52:59.431: E/AndroidRuntime(405):  at android.view.View.performClick(View.java:2485)
02-15 12:52:59.431: E/AndroidRuntime(405):  at android.view.View$PerformClick.run(View.java:9080)
02-15 12:52:59.431: E/AndroidRuntime(405):  at android.os.Handler.handleCallback(Handler.java:587)
02-15 12:52:59.431: E/AndroidRuntime(405):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 12:52:59.431: E/AndroidRuntime(405):  at android.os.Looper.loop(Looper.java:123)
02-15 12:52:59.431: E/AndroidRuntime(405):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-15 12:52:59.431: E/AndroidRuntime(405):  at java.lang.reflect.Method.invokeNative(Native Method)
02-15 12:52:59.431: E/AndroidRuntime(405):  at java.lang.reflect.Method.invoke(Method.java:507)
02-15 12:52:59.431: E/AndroidRuntime(405):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-15 12:52:59.431: E/AndroidRuntime(405):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-15 12:52:59.431: E/AndroidRuntime(405):  at dalvik.system.NativeStart.main(Native Method)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvdisplay"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bprevious"
            android:layout_width="154dp"
            android:layout_height="wrap_content"
            android:text="@string/previous" />

        <Button
            android:id="@+id/bnext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/next" />

    </LinearLayout>

</LinearLayout>

Upvotes: 0

Views: 127

Answers (6)

Gopal Gopi
Gopal Gopi

Reputation: 11131

try this...

        choosenext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                counter++;
                if (counter >= allquotes.length || counter < 0) {
                    counter = 0;
                }
                display.setText(allquotes[counter]);
            }
        });

        chooseprevious.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                counter--;
                if (counter < 0 || counter >= allquotes.length) {
                    counter = 0;
                }
                display.setText(allquotes[counter]);

            }
        });

Upvotes: 1

chipopo
chipopo

Reputation: 326

The application crashes with array out of bounds since you try to access allquotes[Counter] when the counter value is larger then the array size.

Limit the counter or reset it back to zero when it reaches the array size will solve your problem.

Upvotes: 0

Pararth
Pararth

Reputation: 8134

For When all string are finished and still next button is pressed, application crashes,

Counter keeps incrementing and reaches a point where there is no value for that position in the String array.

What happens when you launch app and press previous button straightaway ?. The counter becomes (-1) then. Need to handle minimum and maximum values for the counter depending on the size of the Array.

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93708

Well, the crash is an IndexOutOfBounds problem. onClock increases counter. If counter >= allquotes.length, the app will access an index out of the array and crash. Change onClick to

            if(counter < allquotes.length-1){
                counter++;
            }
            display.setText(allquotes[counter]);

Upvotes: 0

Darcy
Darcy

Reputation: 106

  1. You do not show the first string when the app launched. You can add display.setText(allquotes[0]); somewhere.

  2. Add checking if (counter < allquotes.length) display.setText(allquotes[counter]);

Upvotes: 0

timeshift117
timeshift117

Reputation: 1800

  1. You increment the counter before the text is set, so the allcounter index reffered to is 1 instead of 0.
  2. Without the error log its hard to say what the problem may be. Use the logcat.

Upvotes: 1

Related Questions