将textview内容打印到文件时出错

时间:2014-06-11 11:41:02

标签: android voice-recognition voice-recording

还是相对较新的编码等所以所有的帮助非常感谢。

我有一个简单的语音文本应用程序,工作正常。但是我现在想要将文本视图的内容(打印语音输出的位置)保存到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 info

 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 

1 个答案:

答案 0 :(得分:1)

FILENAME常量为null,这是因为当您通过传递NullPointerException作为参数调用openFileOutput()方法时,FILENAME给出了null。因此,系统无法使用java.lang.NullPointerException at android.app.ContextImpl.makeFilename(ContextImpl.java:2407) android.app.ContextImpl.openFileOutput(ContextImpl.java:1023) at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:192) 创建文件名。日志说清楚。

FILENAME

现在,为除null之外的常量{{1}}提供适当的值,并希望您的问题能够得到解决。