调用函数时模拟器抛出异常(android studio)

Emulator throws exception (android studio) when function is called

我正在 android studio 上编写游戏,它工作正常,但是当我在另一个 class 中移动一个函数时它开始崩溃。

我这样做是为了让我可以在任何地方使用它,只需一次,而不是每次都复制粘贴 class 我需要使用它。

我检查了 google 如何解决这个问题,但没有任何效果,我认为错误是“getExternalFilesDir”或“openFileOutput”返回 null。

这里是 class 有两个功能(基本上做同样的事情)但我不能使用。

import android.media.MediaScannerConnection;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class WriteUserData extends AppCompatActivity {
    
    public void writeToFile(String data, String file) {
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(file + ".txt", MODE_PRIVATE));
            outputStreamWriter.write(data);
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        }
    }

    public void saveFile(String fileName, String addText){
        try {
            // Creates a file in the primary external storage space of the
            // current application.
            // If the file does not exists, it is created.
            File newFile = new File(this.getExternalFilesDir(null), fileName + ".txt");
            if (!newFile.exists())
                newFile.createNewFile();

            // Adds a line to the file
            BufferedWriter writer = new BufferedWriter(new FileWriter(newFile, true /*append*/));
            writer.write(addText);
            writer.close();
            // Refresh the data so it can seen when the device is plugged in a
            // computer. You may have to unplug and replug the device to see the
            // latest changes. This is not necessary if the user should not modify
            // the files.
            MediaScannerConnection.scanFile(this,
                    new String[]{newFile.toString()},
                    null,
                    null);
        } catch (IOException e) {
            Log.e("ReadWriteFile", "Error while trying to add text in " + fileName + ".txt");
        }
    }

}

还有一个地方我尝试调用函数但使模拟器崩溃(我删除了一些代码'import')所以你更容易理解。

    private boolean showStats = true;
    private final static long Interval = 30;
    private Handler handler = new Handler();
    private int flashDisplay = 0;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_over);

        // change game over screen on click
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(showStats){
                            getStats();
                        }
                        flashDisplay++;
                    }
                });
            }
        }, 0, Interval);
    }

    private static void bring(){
        WriteUserData saveScore = new WriteUserData();
        saveScore.saveFile("userdata", "ThisTextWork");
        saveScore.writeToFile("ThisTextWorkToo", "config");
    }

    @SuppressLint("SetTextI18n")
    private void getStats(){
        bring();
    }
}

这是错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference Blockquote

您误解了活动的运作方式。

public class WriteUserData extends AppCompatActivity {

您正在扩展 Activity class,因此 WriteUserData 也是 activity。在那种情况下,它应该这样命名 - WriteUserDataActivity.

但是,看看你在这里做什么:

WriteUserData saveScore = new WriteUserData();

这不是您应该使用 activity 的方式。它应该使用 Intent 和 startActivity() 创建,请参见此处:

https://developer.android.com/training/basics/firstapp/starting-activity

您想要做的是将打开文件的逻辑移到单独的 class 中以避免代码重复。这很好,但它应该 而不是 用 activity 完成。相反,您应该使用 Utils/Helper class。你的 class 不应该扩展任何东西并且应该有一个私有构造函数:

public class WriteUserDataUtils { 
    private WriteUserDataUtils() { 
        // utils classes shouldn't be instanced
    }
    ... 
}

之后,把里面的方法改成static:

public static void writeToFile(String data, String file) {
    ...
}

public static void saveFile(String fileName, String addText) {
    ...
}

最后,扩展 Activity 的原因是某些事情需要 Context(例如打开文件流)。这可以通过将其作为参数传递来轻松解决:

public static void writeToFile(String data, String file, Context context) {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(file + ".txt", MODE_PRIVATE));
    ...
}

public static void saveFile(String fileName, String addText, Context context){
    try {
        File newFile = new File(context.getExternalFilesDir(null), fileName + ".txt");
    ...
}

将其重构为实用程序 class 后,您可以从 activity 中调用它,如下所示:

private static void bring(){
    WriteUserDataUtils.saveFile("userdata", "ThisTextWork", this);
    WriteUserDataUtils.writeToFile("ThisTextWorkToo", "aconfig", this);
}

如果您需要进一步说明,请告诉我。