试图分类

时间:2015-06-05 18:36:23

标签: java android android-intent

所以我正在建立一个关于事实的应用程序。我为不同的活动创建了不同的按钮,我在textview中显示了我的事实。我已经从选择事实的资产中创建了不同的文本文件。问题是每次我点击按钮我的应用程序崩溃。帮帮我们......

我的xml代码

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:id="@+id/n"
    android:layout_centerHorizontal="true"
    android:textSize="35dp" />
<ImageButton
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:id="@+id/btn"
    android:background="@drawable/boombuttonnext"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<ImageButton
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:id="@+id/prev"
    android:background="@drawable/backk"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

我的Java代码

public class MainActivity22Activity extends AppCompatActivity {

    ImageButton btn,prev;
    TextView tc;
    ArrayList<String> listOfFacts;
    int position = 0;

    void init()
    {
        listOfFacts = new ArrayList<String>();

        try {
            InputStream is =  getAssets().open("boom.txt");

            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String line ;
            while( (line =  in.readLine())   != null)
                listOfFacts.add(line);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);
        init();




        position =   getSharedPreferences("storage" ,1).getInt("pos",0);


        btn  = (ImageButton) findViewById(R.id.btn);
        prev  = (ImageButton) findViewById(R.id.prev);
        tc = (TextView) findViewById(R.id.b);

        tc.setText("" + listOfFacts.get(position));





        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                position++;

                if(listOfFacts.size()==position)
                    position = 0;

                tc.setText("" + listOfFacts.get(position));


            }
        });

        prev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                position--;

                if(position<0)
                    position = listOfFacts.size()-1;

                tc.setText("" + listOfFacts.get(position));

            }
        });

    }


    @Override
    protected void onPause() {
        super.onPause();
        getSharedPreferences("storage" ,1).edit().putInt("pos" , position).commit();
    }
}

1 个答案:

答案 0 :(得分:0)

从logcat的输出中,访问阵列时出现问题。确保从数组中获取元素的索引不会超出边界。

  

&GT;   at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)at java.util.ArrayList.get(ArrayList.java:308)at bollywoodfacts.checkinglist.MainActivity2Activity.onCreate(MainActivity2Activity .java:63)at android.app .Activity.performCreate(Activity.java:5937)在android.app.Anstrumentation.caread.nleCreate(Instrumentation.java:1105)android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)android.app.ActivityThread.handleLaunchActivity的android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) ActivityThread.java:2360)

tc.setText("" + listOfFacts.get(position));