這是發生的問題,我隨便輸入一個數字200後按資料寫入,接著按資料讀取程式模擬器就會跳出,本人才剛接觸Android Studio不久,程式也沒有出現錯誤,目前知道發生閃退應該是程式有問題,不過找了好久了還是不知道問題出在哪邊QQ,麻煩各位高手幫幫忙,非常感謝
以下是程式部分:
<MainActivity.java>
package com.example.stream;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private Button medtData, mtxtData, mBtnClear;
private TextView mRead;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtxtData = (Button) findViewById(R.id.button);
medtData = (Button) findViewById(R.id.button2);
mBtnClear = (Button) findViewById(R.id.button3);
mRead = (TextView) findViewById(R.id.read);
editText = (EditText) findViewById(R.id.textView);
medtData.setOnClickListener(Write);
mtxtData.setOnClickListener(Read);
mBtnClear.setOnClickListener(Clear);
}
private View.OnClickListener Write = new View.OnClickListener() {
@Override
public void onClick(View view) {
FileOutputStream fileOut = null;
BufferedOutputStream bufFileOut = null;
try {
fileOut = openFileOutput("DATA", MODE_APPEND);
bufFileOut = new BufferedOutputStream(fileOut);
bufFileOut.write(medtData.getText().toString().getBytes());
bufFileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
private View.OnClickListener Read = new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileInputStream fileIn = openFileInput("DATA");
BufferedInputStream bufFileIn = new BufferedInputStream(fileIn);
byte[] bufBytes = new byte[10];
mtxtData.setText("");
do {
int c = bufFileIn.read(bufBytes);
if (c == -1)
break;
else
mtxtData.append(new String(bufBytes));
} while (true);
bufFileIn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
private View.OnClickListener Clear = new View.OnClickListener() {
@Override
public void onClick(View view) {
FileOutputStream fileOut = null;
try {
fileOut = openFileOutput("DATA", MODE_PRIVATE);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
<activity_main.xml>
<android.support.constraint.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/read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="221dp"
android:layout_marginRight="221dp"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/textView"
android:layout_width="165dp"
android:layout_height="46dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="180dp"
android:layout_marginRight="180dp"
android:autofillHints="password"
android:ems="10"
android:hint="@string/number"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
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_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="21dp"
android:layout_marginEnd="13dp"
android:layout_marginRight="13dp"
android:text="@string/read"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/read" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:layout_marginEnd="14dp"
android:layout_marginRight="14dp"
android:text="@string/write"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/read" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="199dp"
android:layout_marginEnd="42dp"
android:layout_marginRight="42dp"
android:text="@string/clear"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
應該會顯示錯誤訊息吧...