-1

I am trying to make an app on which i can read poems that ive stored through Array . It contains 2 buttons "Next" & "Back"

Everything seems to work fine but when i reach the end of array and i press next it stops working. and vice versa for back key.

    package com.example.haziqsheikhlocal.ghanwapoems;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;


public class GhanwaPoems extends ActionBarActivity {

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

        final TextView myPoem1 = (TextView) findViewById(R.id.myPoem);
         final Button nextButton = (Button) findViewById(R.id.buttonNext);
        final Button backButton = (Button) findViewById(R.id.backButton);



        View.OnClickListener backListen = new View.OnClickListener() {
            public int kK =0;
            @Override
            public void onClick(View v) {
                String[] mPoemBooks = getStrings();


                if (kK <= mPoemBooks.length - 1 || kK >= 1 ){
                    switch (v.getId())   // v is the button that was clicked
                    {
                        case(R.id.buttonNext):
                            kK++;
                            break;
                        case (R.id.backButton):
                            kK--;
                            break;
                        default:   // this will run the same code for any button clicked that doesn't have id of button1 defined in xml

                            break;

                    }
                    int k = kK;
                    String myPoem = "";
                    myPoem = mPoemBooks[k];



                    myPoem1.setText(myPoem);
                }
else {
                    Toast.makeText(getApplicationContext(), " Sorry No More To Show" , Toast.LENGTH_LONG).show();
                }

            }
};
                       backButton.setOnClickListener(backListen);
                       nextButton.setOnClickListener(backListen);

    }

    private String[] getStrings() {
        return new String[]{"","a","b","c","d","e","f"};
    }
}

What i need is a logic or condition so that when i reach to the end of my array it should display "Sorry No More Poems." and vice versa for back button.

and sorry for the messed code its first time i am making something :P

  • My Conditions are wrong i know that but wat should it be ? – Haziq Sheikh Jan 26 '15 at 18:24
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – njzk2 Jan 26 '15 at 18:33
  • do a minimal debugging before asking. – njzk2 Jan 26 '15 at 18:36
  • i tried to debug it !! i could not find the solution – Haziq Sheikh Jan 26 '15 at 19:00
  • you couldn't find that `(kK <= mPoemBooks.length - 1 || kK >= 1 )` does not prevent `kK` to be `0` and therefore to crash in an indexoutofboundsexception? did you run step by step and observed how the variable were set in your program? – njzk2 Jan 26 '15 at 19:04

3 Answers3

1

You if statement is always true. This should work for you:

if (kK <= mPoemBooks.length - 1 && kK >= 0 ){

mthomas
  • 111
  • 1
  • 2
0

It looks like you are getting an index out of bounds error. This means that you are attempting to access an object in the array (mPoemBooks) that doesn't exist. In the case of the backButton you are most likely getting -1 as the index, and with buttonNext you are getting something larger than the array.

To fix this you should be making sure the index is in the bounds

int k = kK;
String myPoem = "";

if (k < 0) {
    k = 0;
}

if (k >= mPoemBooks.length()) {
    k = mPoemBooks.length() -1; //Subtract 1 since arrays are 0 indexed and length is 1 indexed.
}
myPoem = mPoemBooks[k];
RocketSpock
  • 2,031
  • 16
  • 11
0

There's a lot going on here that needs attention:

1: The onCreate() method should only be used to set up activity for things that need to happen at the activities creation. Setting the listener here is fine, but it's poor code practice to define your anonymous classes there

2: It's simpler just to use two listeners, or use the onClick properties in the layout

3: the current poem is an item that belongs to the activity, so it should be a member of that class

4: I assume you're going to want a better persistent store of the items you display, but that's several steps down the road

Here's a quick implementation of the first 3:

public class Poems extends Activity {

    private int currentPoem;
    private TextView mPoemView;
    private String[] mPoems = new String[] {...}

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

        mPoemView = (TextView) findViewById(R.id.myPoem);
        Button nextButton = (Button) findViewById(R.id.nextButton);
        Button backButton = (Button) findViewById(R.id.backButton);

        nextButton.setOnClickListener(mNextListener);
        backButton.setOnClickListener(mBackListener);
    }

    private View.OnClickListerner mNextListener
            = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (currentPoem < mPoems.length - 1) {
                mPoemView.setText(mPoems[++currentPoem])
            }
        }
    };

    private View.OnClickListerner mBackListener
            = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (currentPoem > 0) {
                mPoemView.setText(mPoems[--currentPoem])
            }
        }
    };
}

Best of luck and welcome to coding.

h0x0
  • 485
  • 3
  • 11
  • Actually i tried to make diffrent classes , one to store poems , other to do the switch thing !! but i could not do it ..... I am learning from online tutorials from Teamtree house and Lynda.. so m quite new – Haziq Sheikh Jan 26 '15 at 19:11