I have a tiny tiny problem that I seem incapable to solve by Googling.
I want to add Tabels (a class I made up) to an array.
I tried below method in code and I also tried these variants
- This:
ArrayList<Tabeller> arrTabeller = new ArrayList<>(new Tabeller(1,true)); - And this:
arrTabeller.add(new Tabeller(1,true));
But as soon I try to add something, it crashes.
Questions:
- why and why cant I see why in android studio?
- How would you initialize and add an element to an array with class objects such as my table?
package com.example.mathstairs;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class MathStairMain extends Activity {
FrameLayout gameFrame;
RelativeLayout gameButtons; // Where I add the buttons
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
ArrayList<Tabeller> arrTabeller = new ArrayList<>();
Tabeller e = new Tabeller(1,true);
arrTabeller.add(e); // if commented away this program works, if active it is crashing
}
It is calling on the Tabel class
package com.example.mathstairs;
public class Tabeller {
int tabell; // Tabeller som ska ingå
Boolean use; // Sant eller falskt
public Tabeller(int tabell, Boolean use)
{
this.tabell = tabell;
this.use = use;
}
}
And it works perfectly until I try to add.
--------- beginning of crash
2022-05-13 10:54:14.044 1658-1658/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mathstairs, PID: 1658
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathstairs/com.example.mathstairs.MathStairMain}: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:437)
at com.example.mathstairs.MathStairMain.StartGame(MathStairMain.java:168)
at com.example.mathstairs.MathStairMain.onCreate(MathStairMain.java:46)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2022-05-13 10:54:14.095 1658-1658/? I/Process: Sending signal. PID: 1658 SIG: 9