0
對編碼等方面還比較新,所以都非常感謝。將文本視圖內容打印到文件時出錯
我有一個簡單的語音到文本應用程序工作正常。但是我現在想要將文本視圖的內容(語音輸出的打印位置)保存到一個txt文件中。我會誠實地說,並不是100%肯定從哪裏開始,所以我修改了一個教程。
當我點擊保存按鈕它崩潰的應用程序。代碼中沒有明顯的錯誤,但我一直盯着它好幾天!謝謝大家。
package co.uk.malleymob.voice2text;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.example.voice2text.R;
public class MainActivity extends Activity {
protected static final int RESULT_SPEECH = 1;
protected static final String FILENAME = null;
protected static final String TAG = null;
private ImageButton btnSpeak;
private Button btnSave;
private TextView txtText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtText = (TextView) findViewById(R.id.txtText);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSave = (Button) findViewById(R.id.btnSave);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textToSaveString = txtText.getText().toString();
writeToFile(textToSaveString);
String textFromFileString = readFromFile();
if (textToSaveString.equals(textFromFileString))
Toast.makeText(getApplicationContext(), "both string are equal", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "there is a problem", Toast.LENGTH_SHORT).show();
}
private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e(TAG, "File write failed: " + e.toString());
}
}
private String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput(FILENAME);
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e(TAG, "File not found: " + e.toString());
} catch (IOException e) {
Log.e(TAG, "Can not read file: " + e.toString());
}
return ret;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtText.setText(text.get(0));
}
break;
}
}
}
}
logcat的信息
06-11 12:35:38.225: W/asset(26051): Copying FileAsset 0x788ce398 (zip:/data/app/com.example.voice2text-1.apk:/resources.arsc) to buffer size 2420 to make it aligned.
06-11 12:35:38.665: I/Adreno-EGL(26051): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL4169980)
06-11 12:35:38.665: I/Adreno-EGL(26051): OpenGL ES Shader Compiler Version: 17.01.10.SPL
06-11 12:35:38.665: I/Adreno-EGL(26051): Build Date: 02/04/14 Tue
06-11 12:35:38.665: I/Adreno-EGL(26051): Local Branch:
06-11 12:35:38.665: I/Adreno-EGL(26051): Remote Branch:
06-11 12:35:38.665: I/Adreno-EGL(26051): Local Patches:
06-11 12:35:38.665: I/Adreno-EGL(26051): Reconstruct Branch:
06-11 12:35:39.246: E/ActivityThread(26051): Performing stop of activity that is not resumed: {com.example.voice2text/co.uk.malleymob.voice2text.MainActivity}
06-11 12:35:39.246: E/ActivityThread(26051): java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.example.voice2text/co.uk.malleymob.voice2text.MainActivity}
06-11 12:35:39.246: E/ActivityThread(26051): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3709)
06-11 12:35:39.246: E/ActivityThread(26051): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3796)
06-11 12:35:39.246: E/ActivityThread(26051): at android.app.ActivityThread.access$1100(ActivityThread.java:156)
06-11 12:35:39.246: E/ActivityThread(26051): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
06-11 12:35:39.246: E/ActivityThread(26051): at android.os.Handler.dispatchMessage(Handler.java:102)
06-11 12:35:39.246: E/ActivityThread(26051): at android.os.Looper.loop(Looper.java:157)
06-11 12:35:39.246: E/ActivityThread(26051): at android.app.ActivityThread.main(ActivityThread.java:5872)
06-11 12:35:39.246: E/ActivityThread(26051): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 12:35:39.246: E/ActivityThread(26051): at java.lang.reflect.Method.invoke(Method.java:515)
06-11 12:35:39.246: E/ActivityThread(26051): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
06-11 12:35:39.246: E/ActivityThread(26051): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
06-11 12:35:39.246: E/ActivityThread(26051): at dalvik.system.NativeStart.main(Native Method)
06-11 12:38:12.620: W/dalvikvm(26051): threadid=1: thread exiting with uncaught exception (group=0x41659e18)
06-11 12:38:12.630: E/AndroidRuntime(26051): FATAL EXCEPTION: main
06-11 12:38:12.630: E/AndroidRuntime(26051): Process: com.example.voice2text, PID: 26051
06-11 12:38:12.630: E/AndroidRuntime(26051): java.lang.NullPointerException
06-11 12:38:12.630: E/AndroidRuntime(26051): at android.app.ContextImpl.makeFilename(ContextImpl.java:2407)
06-11 12:38:12.630: E/AndroidRuntime(26051): at android.app.ContextImpl.openFileOutput(ContextImpl.java:1023)
06-11 12:38:12.630: E/AndroidRuntime(26051): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:192)
06-11 12:38:12.630: E/AndroidRuntime(26051): at co.uk.malleymob.voice2text.MainActivity$2.writeToFile(MainActivity.java:92)
06-11 12:38:12.630: E/AndroidRuntime(26051): at co.uk.malleymob.voice2text.MainActivity$2.onClick(MainActivity.java:80)
06-11 12:38:12.630: E/AndroidRuntime(26051): at android.view.View.performClick(View.java:4480)
06-11 12:38:12.630: E/AndroidRuntime(26051): at android.view.View$PerformClick.run(View.java:18673)
06-11 12:38:12.630: E/AndroidRuntime(26051): at android.os.Handler.handleCallback(Handler.java:733)
06-11 12:38:12.630: E/AndroidRuntime(26051): at android.os.Handler.dispatchMessage(Handler.java:95)
06-11 12:38:12.630: E/AndroidRuntime(26051): at android.os.Looper.loop(Looper.java:157)
06-11 12:38:12.630: E/AndroidRuntime(26051): at android.app.ActivityThread.main(ActivityThread.java:5872)
06-11 12:38:12.630: E/AndroidRuntime(26051): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 12:38:12.630: E/AndroidRuntime(26051): at java.lang.reflect.Method.invoke(Method.java:515)
06-11 12:38:12.630: E/AndroidRuntime(26051): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
06-11 12:38:12.630: E/AndroidRuntime(26051): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
06-11 12:38:12.630: E/AndroidRuntime(26051): at dalvik.system.NativeStart.main(Native Method)
06-11 12:38:16.634: D/Process(26051): killProcess, pid=26051
06-11 12:38:16.654: D/Process(26051): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:131 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690