iT邦幫忙

0

關於Stream讀出數值按下Button後會閃退

0607 2019-05-03 21:49:072319 瀏覽

這是發生的問題,我隨便輸入一個數字200後按資料寫入,接著按資料讀取程式模擬器就會跳出,本人才剛接觸Android Studio不久,程式也沒有出現錯誤,目前知道發生閃退應該是程式有問題,不過找了好久了還是不知道問題出在哪邊QQ,麻煩各位高手幫幫忙,非常感謝

https://ithelp.ithome.com.tw/upload/images/20190503/20117372e2kVGDpodl.pnghttps://ithelp.ithome.com.tw/upload/images/20190503/20117372mUZ5QpffNF.png

以下是程式部分:

<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>

asqweff11 iT邦新手 5 級 ‧ 2019-05-06 09:43:53 檢舉
1. bufFileOut.write(medtData.getText().toString().getBytes());
medtData改editText
2. mtxtData.setText("");
mtxtData改mRead
3. mtxtData.append(new String(bufBytes));
mtxtData改mRead
可能是這樣吧...
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
小魚
iT邦大師 1 級 ‧ 2019-05-03 23:11:29

應該會顯示錯誤訊息吧...

小魚 iT邦大師 1 級 ‧ 2019-05-04 08:05:13 檢舉

通常會閃退如果是開新的layout有可能是layout的問題,
LogCat會抓到錯誤的訊息,
從錯誤的訊息去判斷可能的原因吧...

我要發表回答

立即登入回答