0

I'm new to developing mobile apps and I was trying to make an app to sort the names for a gift exchange. In the beginning, I made it work on Eclipse so the plan was to pretty much replicate it on Android Studio, the only issue is that I cannot figure out why the app crash when I try to remove an item from a list.

public class MainActivity extends AppCompatActivity {
List<String> familyGet = Arrays.asList("Adrian","Alan","Ana","Derek","Diego","Gabriel","Ivan","Maggui","Pamela","Melissa","Mirna","Paty","Herminio");
List<String> familyGive = Arrays.asList("Adrian","Alan","Ana","Derek","Diego","Gabriel","Ivan","Maggui","Pamela","Melissa","Mirna","Paty","Herminio");
int randomn;
boolean sameString = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void activated(View view) {
    TextView intercambios = findViewById(R.id.intercambios);
    TextView finalizo = findViewById(R.id.finalizado);


    if(familyGet.size()!=0) {
        Collections.shuffle(familyGive);

        do{
            Random r1 = new Random();
            randomn = r1.nextInt(familyGive.size());
            sameString= familyGet.get(randomn).equals(familyGive.get(randomn));
        }while(sameString);

        familyGive.remove(randomn);
        familyGet.remove(randomn);
    }else{
        finalizo.setText("Both list are empty");
    }
}
}

"activated" is an "onClick" function of a button. The code works totally fine without both ".remove()".

The error I got from the debugger was:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.intercambionavideo, PID: 22427
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:7448)
    at android.view.View.performClickInternal(View.java:7425)
    at android.view.View.access$3600(View.java:810)
    at android.view.View$PerformClick.run(View.java:28305)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    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.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:7448) 
    at android.view.View.performClickInternal(View.java:7425) 
    at android.view.View.access$3600(View.java:810) 
    at android.view.View$PerformClick.run(View.java:28305) 
    at android.os.Handler.handleCallback(Handler.java:938) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    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.UnsupportedOperationException
    at java.util.AbstractList.remove(AbstractList.java:167)
    at com.example.intercambionavideo.MainActivity.actividad(MainActivity.java:77)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
    at android.view.View.performClick(View.java:7448) 
    at android.view.View.performClickInternal(View.java:7425) 
    at android.view.View.access$3600(View.java:810) 
    at android.view.View$PerformClick.run(View.java:28305) 
    at android.os.Handler.handleCallback(Handler.java:938) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    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) 

W/System: A resource failed to call close.

Belli
  • 1
  • 3
  • `The error I got from the debugger ` I see no error. Or an exception that would let your app crash. Post more and different lines from the log. – blackapps Nov 14 '21 at 08:22
  • @blackapps This time i got it from the "Run" console log and it throws UnsupportedOperationException. At the end of the day this was the solution that work best for me "ArrayList familyGet = new ArrayList<>(Arrays.asList());" – Belli Nov 14 '21 at 09:00

1 Answers1

0

As mentioned in the comments, the Arrays.asList( ... ) is the cause of the problem. See the documentation. It creates an unmodifiable list backed by an array so when you try to remove an item you get an exception.

In your example the names are hard-coded. One way around this, if they're always going to be hard-coded, is to wrap the unmodifiable array list, for example:

List<String> familyGive = new ArrayList(
    Arrays.asList( ... )
);

Instead of having the names hard-coded, write a method to get the names you need from your datasource. Are the names stored in a database? Are they exposed by a rest API? Whatever it is, make a method to fetch them:

List<String> names = initNames();

where you've defined the method accordingly.

From there you can create the two identical lists by wrapping the names returned from your init method: new ArrayList(names).

Also: to see the error in action, run your code in debug and put a breakpoint in your code as-is.

geco17
  • 4,824
  • 1
  • 16
  • 32