- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用以下代码来创建和写入文件
FileOutputStream fOut = mcontext.openFileOutput("msg.txt",Context.MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("abc");
osw.flush();
osw.close();
但是在 LOG cat 中,它在上述代码的第一行给出了空指针异常。
这是完整的代码
public class Server implements Runnable
{
private String address ="127.0.0.1";
private String add="127.0.0.2";
private static Context mcontext;
byte[] buf = new byte[256];
DatagramSocket socket;
@Override
public void run()
{
try{
InetAddress serveradd = InetAddress.getByName(address);
InetAddress clientadd = InetAddress.getByName(add);
socket=new DatagramSocket(6000,serveradd);
Log.d("UDP", "S: Connecting...");
DatagramPacket packet=new DatagramPacket(buf,buf.length);
socket.receive(packet);
Log.d("UDP", "S: Receiving...");
/* Receive the UDP-Packet */
String data=new String(packet.getData());
String phno=data.substring(0,9);
String identity=data.substring(10,14);
//FileWriter f = new FileWriter("/sdcard/download/msg.txt");
// I/O operation
FileOutputStream fOut = mcontext.openFileOutput("msg.txt",Context.MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("gfdffg");
osw.flush();
osw.close();
Log.d("UDP", "S: ReceivedJI: '" + new String(packet.getData()) + "'");
Log.d("UDP", "S: Done.");
//Toast.makeText(context.getApplicationContext(),"received"+new String(packet.getData()),Toast.LENGTH_LONG).show();
// if(TextUtils.isDigitsOnly(phno) && identity.equals("novel"))
// {
byte[] bufsend =("hi").getBytes();
Log.d("UDP", "S: sending.");
DatagramPacket packetsend=new DatagramPacket(bufsend,bufsend.length,clientadd,5000);
socket.send(packetsend);
Log.d("UDP", "S: send.'"+bufsend+"'");
//}
//else
// {
//Log.d("UDP", "S: not matched.");
// }
}
catch (Exception e)
{
Log.e("UDP", "S: Error", e);
e.printStackTrace();
//Toast.makeText(context.getApplicationContext(),"received"+e,Toast.LENGTH_LONG).show();
}
}
}
任何帮助将不胜感激
最佳答案
当您使用mcontext
时,您没有在代码中的任何位置设置它。所以变量是null
。您应该为您的 Runnable
提供一个接受 Context
的构造函数:
public Server(Context context) {
mcontext = context;
}
因此您可以设置 mcontext 变量。另外,不要使其静态。
关于java - Openfileinput 返回空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12303864/
我使用以下代码来创建和写入文件 FileOutputStream fOut = mcontext.openFileOutput("msg.txt",Context.MODE_WORLD_READAB
我正在尝试读取文本文件。到目前为止,我已经使用 BufferReader 获得了代码。但在我使用 openFileInput("Storyline.txt") 的地方,它会给出错误:The metho
我有这个代码: package com.example.android.game; import android.content.Context; import android.graphics.Ca
我添加了用于文件浏览的外部库。该库返回选定的文件路径键,但不返回值。 package com.example.dev.nordugrid; import com.orleonsoft.android.
我正在尝试为学校项目编写一个基本的“记事本”应用程序。 我用 editText 创建了主类,我将它保存为 String textOutput。 我使用以下方法将字符串保存到文件中: FileOutpu
要从应用程序的私有(private)存储区读取 Android 中的文件,请使用函数openFileInput()。 我的问题是,有没有办法在调用这个函数之前检查这个文件是否存在?该函数可以抛出 Fi
我正在尝试使用 context.getContentResolver().openInputStream(Uri) 打开多个线程的多个文件但似乎在读取时,所有线程都从同一个文件读取。我尝试过使用方法
我正在尝试让我的 rss 阅读器保存 URL 地址,以便在重新打开时保存它。但出于同样的原因,openFileOutput 和 openFileInput 是红色的,它表示它无法解析方法。 我们的教授
所以,我正在尝试从 android internal private storage 加载一个预存储的 XML 文件。并将其解析为 JDOM 文档。 XML 与以下相同;
我正在尝试编写/创建一个文件,但是无论我如何尝试,我都会遇到 filenotfound 异常。我已经尝试了所有这些: FileInputStream is = openFileInput(filena
package com.example.tictactoeshowgrid; import android.os.Bundle; import java
我在代码行中收到“无法解析方法”错误: FileInputStream fis = openFileInput(fileName); 我不参加 Activity 我在一个单独的类(class)所以我假
目前,我有以下代码用于保存 Web 存档,然后将其作为 FileInputStream 获取。但是,webContent 中的 channel 仍然为空,并抛出 FileNotFoundExcepti
我一直在玩弄 android 平台,玩弄不同的数据存储方式。现在我正在使用 Context 方法 openFileInput() 和 openFileOutput()。 正如这两种方法的文档告诉我的那
我有一个 Android Service 每天运行,它会进行一些数据同步。它每天下载一次文件并通过 context.openFileOutput 将其缓存到磁盘: String fileName =
在我的 MainActivity.java 中,我试图根据代码 given here 打开一个文件.但是在运行代码的时候 public class MainActivity extends AppCo
我是一名优秀的程序员,十分优秀!