android将数据写入文件内部,类中的方法

时间:2013-11-11 11:08:07

标签: java android file class storage

我尝试使用android的内部存储器在txt文件中写入数据, 当我在mainActivity.java中的“buttonClicked”函数中直接使用代码时它工作正常,但是当我尝试在类中调用该方法时,模拟器崩溃。谢谢!

public class MainActivity extends Activity {

        // the class object ----------------------
    FileHandler myFileHandler = new FileHandler();
    public static final String FILENAME = "myFile.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


   public void buttonClicked(View view) {
       // Call the method in the Filehandler Class 
       myFileHandler.saveData(FILENAME);
} ...

fileHandler类

public class FileHandler extends Activity {
    private static final String TAG = MainActivity.class.getName();

    FileHandler() { 
    }


    void saveData(String FILENAME) {

        Log.d(TAG, FILENAME);

        try {
               OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
               outputStreamWriter.write("hallo welt");
               outputStreamWriter.close();
           }
           catch (IOException e) {
               Log.e(TAG, "File write failed: " + e.toString());
           }
    } ...

这很好......

public void buttonClicked(View view) {



       myFileHandler.saveData(FILENAME);

        try {
               OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
               outputStreamWriter.write("hallo welt");
               outputStreamWriter.close();
           }
           catch (IOException e) {
               Log.e(TAG, "File write failed: " + e.toString());
           } 

    }

正确导入所有类,log cat

中的错误
    11-11 11:49:22.646: E/AndroidRuntime(833): FATAL EXCEPTION: main
11-11 11:49:22.646: E/AndroidRuntime(833): java.lang.IllegalStateException: Could not execute method of the activity
11-11 11:49:22.646: E/AndroidRuntime(833):  at android.view.View$1.onClick(View.java:2072)
11-11 11:49:22.646: E/AndroidRuntime(833):  at android.view.View.performClick(View.java:2408)
11-11 11:49:22.646: E/AndroidRuntime(833):  at android.view.View$PerformClick.run(View.java:8816)
11-11 11:49:22.646: E/AndroidRuntime(833):  at android.os.Handler.handleCallback(Handler.java:587)
11-11 11:49:22.646: E/AndroidRuntime(833):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 11:49:22.646: E/AndroidRuntime(833):  at android.os.Looper.loop(Looper.java:123)
11-11 11:49:22.646: E/AndroidRuntime(833):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-11 11:49:22.646: E/AndroidRuntime(833):  at java.lang.reflect.Method.invokeNative(Native Method)
11-11 11:49:22.646: E/AndroidRuntime(833):  at java.lang.reflect.Method.invoke(Method.java:521)
11-11 11:49:22.646: E/AndroidRuntime(833):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-11 11:49:22.646: E/AndroidRuntime(833):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-11 11:49:22.646: E/AndroidRuntime(833):  at dalvik.system.NativeStart.main(Native Method)
11-11 11:49:22.646: E/AndroidRuntime(833): Caused by: java.lang.reflect.InvocationTargetException
11-11 11:49:22.646: E/AndroidRuntime(833):  at com.example.iLoveMyMobile.MainActivity.buttonClicked(MainActivity.java:55)
11-11 11:49:22.646: E/AndroidRuntime(833):  at java.lang.reflect.Method.invokeNative(Native Method)
11-11 11:49:22.646: E/AndroidRuntime(833):  at java.lang.reflect.Method.invoke(Method.java:521)
11-11 11:49:22.646: E/AndroidRuntime(833):  at android.view.View$1.onClick(View.java:2067)
11-11 11:49:22.646: E/AndroidRuntime(833):  ... 11 more
11-11 11:49:22.646: E/AndroidRuntime(833): Caused by: java.lang.NullPointerException
11-11 11:49:22.646: E/AndroidRuntime(833):  at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
11-11 11:49:22.646: E/AndroidRuntime(833):  at com.example.iLoveMyMobile.FileHandler.saveData(FileHandler.java:36)
11-11 11:49:22.646: E/AndroidRuntime(833):  ... 15 more

2 个答案:

答案 0 :(得分:0)

中的问题是context object became null
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE)); 

此处openFileOutput实际上是this.openFileOutput 因此,openFileOutput方法指的是MainActivity的相同上下文,但是当您放入另一个活动时,它将上下文视为null。您可以通过传递context参数来重写该方法 例如

void saveData(String FILENAME, Context mContext) {

        Log.d(TAG, FILENAME);

        try {
               OutputStreamWriter outputStreamWriter = new OutputStreamWriter(mContext.openFileOutput(FILENAME, Context.MODE_PRIVATE));
               outputStreamWriter.write("hallo welt");
               outputStreamWriter.close();
           }
           catch (IOException e) {
               Log.e(TAG, "File write failed: " + e.toString());
           }
    }

在打电话时你可以写下以下内容

myFileHandler.saveData(FILENAME, this);

答案 1 :(得分:0)

如果我理解你正确,你试图在MainActivity的FileHandler类中使用该方法。如果FileHandler只是一个用于文件存储的对象,它不应该扩展Activity。

删除extends activity并将您的Mainactivity用作FileHandler类的上下文。

    FileHandler f = new FileHandler(Mainactivity.class);

然后在FileHandler

    context.openFileOutput()