android方法异常抛出异常,当函数被调用时,模拟器抛出异常(android studio

我正在android studio上编写一个游戏,它工作得很好,但是当我把一个函数移到另一个类中时,它就开始崩溃了。

我这样做是为了让我可以在任何地方使用它,因为我只拥有一次它,而不是复制粘贴到我需要使用它的每一个类上。

我在谷歌上查看了如何解决这个问题,但没有任何工作,错误是“getExternalFilesDir”或“OpenFileOutput”返回空我想。

这是一个有两个函数的类(做的事情基本相同),但我不能使用。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:尝试在空对象引用块Quote上调用虚拟方法“java.io.FileOutputStream Android.content.context.OpenFileOutput(java.lang.String,int)”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值