Вот мой 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
Я новичок в android разработке, и я пытаюсь создать базовое c приложение для формул использования предиктора посещаемости. Теперь, когда я пытаюсь получить значения из edittext в основной деятельности, когда я нажимаю кнопку отправки, приложение закрывается автоматически Пожалуйста, помогите мне решить эту проблему:)
Основная деятельность:
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);
}
}
Вторая деятельность:
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);
}
});
}
}
Третья деятельность:
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 манифест:
<?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>