MainActivity:
package com.example.jaypatel.file;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.Buffer;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void writeData(View view)
{
BufferedWriter bufferWriter = null;
try {
FileOutputStream fileOutputStream = openFileOutput("testFile", Context.MODE_PRIVATE);
bufferWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
bufferWriter.write(((EditText)this.findViewById(R.id.editText1)).getText().toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally
{
try {
bufferWriter.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
public void readData(View view)
{
BufferedReader bufferedReader = null;
StringBuilder result = new StringBuilder();
try {
FileInputStream fileInputStream = openFileInput("testFile");
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line;
while ((line=bufferReader.readLine())!=null){
result.append(line + "\r\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
((EditText)this.findViewById(R.id.editText2)).setText(result);
}
}
错误日志:
FATAL EXCEPTION: main
Process: com.example.jaypatel.file, PID: 3382
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.jaypatel.file.MainActivity.readData(MainActivity.java:65)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我收到了你的错误,因为你声明了两个时间缓冲读取器,因此它显示空指针异常,
只需查看以下编辑后的代码即可
public void readData(View view)
{
BufferedReader bufferedReader = null;
StringBuilder result = new StringBuilder();
try {
FileInputStream fileInputStream = openFileInput("testFile");
bufferReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line;
while ((line=bufferReader.readLine())!=null){
result.append(line + "\r\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
((EditText)this.findViewById(R.id.editText2)).setText(result);
}
答案 1 :(得分:0)
问题在于editText2
,您尚未对其进行初始化。我希望你做以下事情:
private EditText editText;
将视图(editText2)
绑定在onCreate()
方法中,如下所示
editText = (EditText) findViewById(R.id.editText2);
在readData()
内而不是
((EditText)this.findViewById(R.id.editText2)).setText(result);
使用以下代码行:
this.editText.setText(result);