Home » Android » android – App closing automatically on button click in main activity-Exceptionshub

android – App closing automatically on button click in main activity-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

Here is my logcat:

53:46.592 994-994/com.example.attendancepredictor E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.attendancepredictor, PID: 994
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.attendancepredictor/com.example.attendancepredictor.third_activity}: java.lang.ArithmeticException: divide by zero
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3138)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3289)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2012)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:227)
        at android.app.ActivityThread.main(ActivityThread.java:7211)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:575)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:903)
     Caused by: java.lang.ArithmeticException: divide by zero
        at com.example.attendancepredictor.third_activity.onCreate(third_activity.java:31)
        at android.app.Activity.performCreate(Activity.java:7315)
        at android.app.Activity.performCreate(Activity.java:7306)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3118)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3289) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2012) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:227) 
        at android.app.ActivityThread.main(ActivityThread.java:7211) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:575) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:903) 
2020-02-18 17:53:46.610 994-994/? I/Process: Sending signal. PID: 994 SIG: 9

Im new to android development and i am trying to make a basic app for attendance predictor use formulas.Now when i try to get the values from edittext in the main activity when i click on submit button,the app is closing automatically.
please help me solve this
thanks 🙂

Main activity:

package com.example.attendancepredictor;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {
    Button button;
    EditText send_text;
    EditText send_text2;

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

        Button button = (Button) findViewById(R.id.button);
        send_text = (EditText) findViewById(R.id.editText2);
        send_text2 = (EditText) findViewById(R.id.editText);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // get the value which input by user in EditText
                // and convert it to string
                double n1 = Double.valueOf(send_text.getText().toString());
                double n2 = Double.valueOf(send_text2.getText().toString());

                // Create the Intent object of this class Context() to Second_activity class
                Intent intent = new Intent(getApplicationContext(), third_activity.class);

                // now by putExtra method put the value in key, value pair
                // key is message_key by this key we will receive the value, and put the string

                intent.putExtra("message_key", n1);
                intent.putExtra("message_key1", n2);

                // start the Intent
                startActivity(intent);
            }
        });
    }

    public void goTosecond_activity (View view){
        Intent intent = new Intent(this, second_activity.class);
        startActivity(intent);
    }
}

Second activity:

package com.example.attendancepredictor;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class second_activity extends AppCompatActivity {
    Button predict_button;
    EditText send_text3;
    EditText send_text4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_activity);
        Button predict_button= (Button)findViewById(R.id.button2);
        send_text3 = (EditText)findViewById(R.id.editText4);
        send_text4=(EditText)findViewById(R.id.editText5);
        predict_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // get the value which input by user in EditText
                // and convert it to string
                double n3 = Double.valueOf(send_text3.getText().toString());
                double n4 = Double.valueOf(send_text4.getText().toString());

                // Create the Intent object of this class Context() to Second_activity class
                Intent intent = new Intent(getApplicationContext(), third_activity.class);

                // now by putExtra method put the value in key, value pair
                // key is message_key by this key we will receive the value, and put the string

                intent.putExtra("message_key2", n3);
                intent.putExtra("message_key3", n4);

                // start the Intent
                startActivity(intent);
            }
        });    
    }
}

third activity:

package com.example.attendancepredictor;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class third_activity extends AppCompatActivity {

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

            receiver_msg = (TextView)findViewById(R.id.textView8);

            // create the get Intent object
            Intent intent = getIntent();

            // receive the value by getStringExtra() method
            // and key must be same which is send by first activity
            int n1 = intent.getIntExtra("message_key",0);
            int n2 = intent.getIntExtra("message_key1",0);
            int n3 = intent.getIntExtra("message_key2",0);
            int n4 = intent.getIntExtra("message_key3",0);

            double n5 = ((n1+n3)/(n2+n3))*100;
            double n6 = ((n1)/(n2+n4))*100;

            // display the string into textView
            receiver_msg.setText(""+n5);
            receiver_msg.setText(""+n6);

        }
    }

android manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.attendancepredictor">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".third_activity"
           android:label="@string/app_name" >

        </activity>
        <activity android:name=".second_activity"
            android:label="@string/app_name">
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
How to&Answers:

The values you are getting from the extra is 0, hence you are getting exception when you try to divide from zero.

Use the following code to make the application not crash:

try{
double n5 = ((n1+n3)/(n2+n3))*100;
            double n6 = ((n1)/(n2+n4))*100;
}catch(Exception exec){
   exec.printStacktrace
}

You will have to debug why you are getting zero and not the actual value which you are sending to fix the issue.

As I see you are sending two values from second activity to third activity and two values from Main Activity so always two values will be zero.

Hope this helps.