0

我尝试使用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);
} ...

文件处理程序类

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());
           }
    } ...

这很好用……</p>

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
4

2 回答 2

0

如果我理解正确,您正在尝试使用 MainActivity 的 FileHandler 类中的方法。如果 FileHandler 只是用于文件保存的对象,则不应扩展 Activity。

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

    FileHandler f = new FileHandler(Mainactivity.class);

然后在 FileHandler

    context.openFileOutput()
于 2013-11-11T11:19:12.880 回答
0

问题context object became null出在

OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE)); 

这里openFileOutput实际上是this.openFileOutput SoopenFileOutput方法指的是 MainActivity 的相同上下文,但是当您放入另一个活动时,它会将上下文设为空。您可以通过传递上下文参数来重写方法,例如

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);
于 2013-11-11T11:18:28.060 回答