Android Studio после нажатия на кнопку вылетает
Создаю приложение на Android Studio, где задаю значение и диапазон, при нажатии на кнопку button3 значения сохраняются в файл, при нажатии на Button2 должно перейти на другую Activity и выдать мне график. При нажатии на кнопку вылетает. В чем может быть причина?
MainActivity
package com.example.lab_43;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v) {
EditText B = (EditText) findViewById(R.id.b);
EditText F = (EditText) findViewById(R.id.from);
EditText T = (EditText) findViewById(R.id.to);
EditText S = (EditText) findViewById(R.id.step);
double b = Double.parseDouble(B.getText().toString());
double y = 3;
double a = 8;
double from = Double.parseDouble(F.getText().toString());
double to = Double.parseDouble(T.getText().toString());
double step = Double.parseDouble(S.getText().toString());
try {
FileOutputStream fileOutput = openFileOutput("output.txt", MODE_PRIVATE);
for (double i = from; i <= to; i += step) {
double x = i;
double res = (Math.sqrt(b + x - a) + Math.log(y)) / Math.atan(b + a);
String X = String.format("%.3f", x);
String result = String.format("%.3f", res);
fileOutput.write(("X = " + X + " -> K = " + result + ";\n").getBytes());
}
fileOutput.close();
Toast.makeText(this, "Save", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException exception) {
Toast.makeText(this, "Error",
Toast.LENGTH_SHORT).show();
} catch (IOException exception) {
Toast.makeText(this, "Error",
Toast.LENGTH_SHORT).show();
}
}
public void onButtonClickFile(View v) {
TextView data = (TextView) findViewById(R.id.data);
try {
FileInputStream fileInput = openFileInput("output.txt");
InputStreamReader reader = new InputStreamReader(fileInput);
BufferedReader buff = new BufferedReader(reader);
StringBuffer strbuff = new StringBuffer();
String line;
while ((line = buff.readLine()) != null) {
strbuff.append(line.toString()).append("\n");
}
data.setText(strbuff.toString());
} catch (FileNotFoundException e) {
Toast.makeText(this, "Error",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Error",
Toast.LENGTH_SHORT).show();
}
}
public void onButtonClickShowPlot(View v) {
Intent intent = new Intent("com.example.lab_43.Graphic");
startActivity(intent);
}
}
Graphic
package com.example.lab_43;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Graphic extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graphic);
GraphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>();
try {
FileInputStream fileInput = openFileInput("output.txt");
InputStreamReader reader = new InputStreamReader(fileInput);
BufferedReader buff = new BufferedReader(reader);
String line;
while ((line = buff.readLine()) != null) {
double[] numArr = Arrays.stream(line.split(" -> result = ")).mapToDouble(Double::parseDouble).toArray();
series.appendData(new DataPoint(numArr[0], numArr[1]), true, 100);
}
} catch (FileNotFoundException e) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
graph.addSeries(series);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/data"
android:layout_width="223dp"
android:layout_height="91dp"
android:layout_marginStart="32dp"
android:layout_marginBottom="104dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/b"
android:layout_width="285dp"
android:layout_height="75dp"
android:layout_marginStart="15dp"
android:layout_marginTop="90dp"
android:layout_marginBottom="72dp"
android:backgroundTint="#CDDC39"
android:ems="10"
android:hint="Введите b"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="@+id/from"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="77dp"
android:backgroundTint="#FFC107"
android:onClick="onButtonClickFile"
android:text="Показать файл"
app:layout_constraintEnd_toEndOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<EditText
android:id="@+id/from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="189dp"
android:layout_marginEnd="69dp"
android:backgroundTint="#8BC34A"
android:ems="10"
android:hint="От"
android:inputType="number"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/step"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:layout_marginEnd="8dp"
android:backgroundTint="#8BC34A"
android:ems="10"
android:hint="Шаг"
android:inputType="number"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/to"
app:layout_constraintTop_toBottomOf="@+id/to" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="74dp"
android:layout_marginEnd="34dp"
android:backgroundTint="#E91E63"
android:onClick="onButtonClickShowPlot"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/step" />
<EditText
android:id="@+id/to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:backgroundTint="#8BC34A"
android:ems="10"
android:hint="К"
android:inputType="number"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="@+id/from"
app:layout_constraintTop_toBottomOf="@+id/from" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="23dp"
android:backgroundTint="#CDDC39"
android:onClick="onButtonClick"
android:text="Расчитать"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="@+id/step" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_graphic.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Graphic">
<com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>