Home > OS >  My android app (java) is crashing immediately after performing a task
My android app (java) is crashing immediately after performing a task

Time:10-02

I was trying to create an automatic sms-sending app, which sends SMSs to 4-5 people through a loop. When I click the button which should execute the code, app immediately crashes, but messages are sent & received successfully when I check my phone's messaging app, which seems strange to me. Here's the code I built :

package com.ambanagri.smsapp;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;

import androidx.core.app.ActivityCompat;

public class MainActivity extends Activity {
    private EditText editTextNumber;
    private EditText editTextMessage;
    private TextView labelDisplay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS, Manifest.permission.READ_SMS}, PackageManager.PERMISSION_GRANTED);
        editTextMessage = findViewById(R.id.editText2);
        editTextNumber = findViewById(R.id.editText1);
        labelDisplay = findViewById(R.id.label);
    }

    public void sendSMS(View view){
        String message = editTextMessage.getText().toString();
        String number = editTextNumber.getText().toString();

        String[] recipients = message.split(", ");
        String[] numbers = number.split(", ");
        final String[] finalmessage = new String[1];
        final String[] displayMessage = new String[1];
        SmsManager mySmsManager = SmsManager.getDefault();
        Handler handler1 = new Handler();

        for(int i=0;i<=numbers.length;i  ){
            int finalI = i;
            handler1.postDelayed(new Runnable() {
                @Override
                public void run() {
                    finalmessage[0] = "Hi there, "   recipients[finalI];
                    mySmsManager.sendTextMessage(numbers[finalI],null, finalmessage[0], null, null);
                    displayMessage[0] = "Sent to - "   finalI   "/"   numbers.length;
                    labelDisplay.setText(displayMessage[0]);
                }
            }, 1000);
        }

        Context context = getApplicationContext();
        CharSequence text = "Message successfully sent to "   numbers.length   "people.";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
}

I earlier thought the for-loop I am using is getting too much data to process, so I tried adding a 1s delay - still, the app is crashing immediately. I wonder what's the reason behind my app crashing, as the messages are sent and received successfully...

Your advice/help is really appreciated! Thanks!

CodePudding user response:

If you are working on Android Studio, look into the error logs and locate the error corresponding to the crash, this should give you/us main and fast leads to save the issue.

The fact that the message is actually sent just mean the crash happens in the rest of the code after the line sending the message.

For example, from what I remember labelDisplay.setText(displayMessage[0]); cannot be executed here since you are trying to modify an element of the main view outside of ui thread: probably crashing the App.

Using runOnUiThread() could fix your issue.

EDIT (some more info):

postDelayed is not waited, it is executed asynchronously which means that the code after it will execute even if the code does not end. The toast is displayed before all the threads created by handler1 end. I am not sure why you want to delay the messages. Make sure you really need postDelayed.

Your loop is going too far, the indexes of a list are going from 0 to length-1, therefore you should write:

 for(int i=0;i<numbers.length;i  )

The last iteration of this for loop is probably your second reason of crash.

  • Related