Search code examples
androidtoast

Android: Help! Application has stopped working


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


Solution

  • 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.