gpt4 book ai didi

java - OpenFileOutput() 得到一个 nullPointerException

转载 作者:行者123 更新时间:2023-11-30 02:13:51 26 4
gpt4 key购买 nike

我创建了一个静态处理程序来从监听传入蓝牙数据的线程接收数据。此处理程序调用:measure_finish2() 并且此方法调用一个名为:measure_finish() 的非静态方法,在最后一个方法中我需要调用 openFileOutput 方法但我有一个 nullPointerException,即使我调用一个简单的 Toast 我也有相同的方法nullPointrException,这是我的代码的一部分:

    public class MainActivity Extends Activity{
...
public void sending_tubes_to_clean(){
stream_cleaning_tubes = "15";
for(int i = 0;i<4;i++){
if(itemsChecked[i] == true){
stream_cleaning_tubes += "," + Integer.toString(i+1);
}
}
if(conectToServerThread != null){
conectToServerThread.commsThread.write(stream_cleaning_tubes);
Toast.makeText(getBaseContext(), "Cleaning the selected Tubes!", Toast.LENGTH_SHORT).show();
cleaning_status();
}else{
Toast.makeText(getBaseContext(), "You must be connected to the Viscometer!...", Toast.LENGTH_SHORT).show();
}
Log.d(tag,stream_cleaning_tubes);
}
public static Handler MainReceiver = new Handler(){
@Override
public void handleMessage(Message msg){
int numOfBytesReceived = msg.arg1;
byte[] buffer = (byte[]) msg.obj;
String strReceived = new String(buffer);
String comand;
strReceived = strReceived.substring(3,numOfBytesReceived);
comand = strReceived.substring(0,2);

if(comand.toString().equals("03")){
//MainActivity o = new MainActivity();
measure_finish2(strReceived.substring(3));
//measure_finish2(strReceived.substring(3));
}

}
};
public static String get_data(String aux){
String aux2 = null;
aux2 = aux.substring(0,aux.indexOf(","));
return aux2;
}

public static void measure_finish2(String data){
MainActivity o = new MainActivity();
o.measure_finish(data);
}
public void measure_finish(String data){
String data_to_save,strdate,tube,temp,time,cte_tube,visco;
String file_name = "results.txt";
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
strdate = sdf.format(c.getTime());
tube = get_data(data);
data = data.substring(data.indexOf(",")+1);
temp = get_data(data);
data = data.substring(data.indexOf(",")+1);
time = get_data(data);
data = data.substring(data.indexOf(",")+1);
cte_tube = get_data(data);
data = data.substring(data.indexOf(",")+1);
visco = get_data(data);
data_to_save = sample_id_var + user_id_var + strdate + "Tube# " + tube + temp + "°C"+ cte_tube + time + "s" + visco + "cSt" + "\n";
try{
FileOutputStream fos = openFileOutput(file_name,MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
try {
osw.write(data_to_save);
osw.flush();
osw.close();
}catch (IOException e) {
e.printStackTrace();
}

//Toast.makeText(getBaseContext(), "F saved", Toast.LENGTH_SHORT).show();
}catch(FileNotFoundException e)
{
e.printStackTrace();
}

samp_id_view.setText(data_to_save);
cleaning_status();
}

请任何人帮助我,这是我遇到的错误:

 04-17 17:09:18.926: E/AndroidRuntime(1685): FATAL EXCEPTION: main
04-17 17:09:18.926: E/AndroidRuntime(1685): Process: com.callirgos.viscopi, PID: 1685
04-17 17:09:18.926: E/AndroidRuntime(1685): java.lang.IllegalStateException: Could not execute method of the activity
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.view.View$1.onClick(View.java:4007)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.view.View.performClick(View.java:4756)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.view.View$PerformClick.run(View.java:19749)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.os.Handler.handleCallback(Handler.java:739)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.os.Handler.dispatchMessage(Handler.java:95)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.os.Looper.loop(Looper.java:135)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-17 17:09:18.926: E/AndroidRuntime(1685): at java.lang.reflect.Method.invoke(Native Method)
04-17 17:09:18.926: E/AndroidRuntime(1685): at java.lang.reflect.Method.invoke(Method.java:372)
04-17 17:09:18.926: E/AndroidRuntime(1685): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-17 17:09:18.926: E/AndroidRuntime(1685): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-17 17:09:18.926: E/AndroidRuntime(1685): Caused by: java.lang.reflect.InvocationTargetException
04-17 17:09:18.926: E/AndroidRuntime(1685): at java.lang.reflect.Method.invoke(Native Method)
04-17 17:09:18.926: E/AndroidRuntime(1685): at java.lang.reflect.Method.invoke(Method.java:372)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.view.View$1.onClick(View.java:4002)
04-17 17:09:18.926: E/AndroidRuntime(1685): ... 10 more
04-17 17:09:18.926: E/AndroidRuntime(1685): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:181)
04-17 17:09:18.926: E/AndroidRuntime(1685): at com.callirgos.viscopi.MainActivity.measure_finish(MainActivity.java:434)
04-17 17:09:18.926: E/AndroidRuntime(1685): at com.callirgos.viscopi.MainActivity.measure_finish2(MainActivity.java:415)
04-17 17:09:18.926: E/AndroidRuntime(1685): at com.callirgos.viscopi.MainActivity.test_files(MainActivity.java:478)
04-17 17:09:18.926: E/AndroidRuntime(1685): ... 13 more

最佳答案

MainActivity o = new MainActivity();

首先,永远不要自己创建一个Activity的实例。 Android 的框架创造了这些,而不是你。此 o 未正确初始化,因此任何尝试将其用于 openFileOutput()Toast 等通常都会崩溃.

其次,不要在主应用程序线程上进行磁盘 I/O。 measure_finish() 中显示的代码应该由蓝牙线程或另一个后台线程完成,而不是主应用程序线程。

关于java - OpenFileOutput() 得到一个 nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29709725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com