Android: 求助!应用程序已停止工作

Android: Help! Application has stopped working

我正在尝试制作一个应用程序,我可以在上面阅读我通过 Array 存储的诗歌。 它包含 2 个按钮 "Next" & "Back"

一切似乎都工作正常,但是当我到达数组末尾并按下下一步时它停止工作。反之亦然。

    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"};
    }
}

我需要的是一个逻辑或条件,以便当我到达数组末尾时它应该显示 "Sorry No More Poems.",反之亦然用于后退按钮。

抱歉我第一次做东西时代码乱七八糟 :P

你的if语句总是为真。这应该适合你:

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

您似乎遇到了索引越界错误。这意味着您正在尝试访问数组 (mPoemBooks) 中不存在的对象。在 backButton 的情况下,你很可能得到 -1 作为索引,而对于 buttonNext 你得到的东西比数组更大。

要解决此问题,您应该确保索引在范围内

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];

这里有很多事情需要注意:

1: onCreate() 方法应该只用于设置 activity 活动创建时需要发生的事情。在此处设置侦听器很好,但是在此处定义匿名 classes

是糟糕的代码实践

2: 使用两个监听器更简单,或者在布局中使用onClick属性

3: 当前的诗是属于 activity 的项目,所以它应该是 class

的成员

4:我假设您想要更好地持久存储所显示的项目,但这需要几个步骤

这是前 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])
            }
        }
    };
}

祝你好运,欢迎编码。