Android 在服务类中检索文件时如何解决空指针异常?
我在服务类中使用的文件utils不是从其路径检索文件,而是当我在活动中使用相同的文件utils时,它会根据需要执行。 那么,谁能为我的问题提供正确的答案呢 这些是我使用过的编程行Android 在服务类中检索文件时如何解决空指针异常?,android,android-activity,service,difference,Android,Android Activity,Service,Difference,我在服务类中使用的文件utils不是从其路径检索文件,而是当我在活动中使用相同的文件utils时,它会根据需要执行。 那么,谁能为我的问题提供正确的答案呢 这些是我使用过的编程行 public class MyService extends Service { IBinder mBinder; private File root; private ArrayList<File> fileList = new ArrayList<File>();
public class MyService extends Service
{
IBinder mBinder;
private File root;
private ArrayList<File> fileList = new ArrayList<File>();
@Override
public void onCreate()
{
root = new File(Environment.getExternalStorageDirectory() + "/user/");
getfile(root);
for (int i = 0; i < fileList.size(); i++)
{
if (fileList.get(i).isDirectory()) {
File userPath = new File(fileList.get(i).getPath());
File[] uFiles = userPath.listFiles();
for (int k = 0; k < uFiles.length; k++) {
File f1 = new File(uFiles[k].toString());
String comFile = Environment.getExternalStorageDirectory() + "/Download/" + uFiles[k].getName();
// System.out.println(comFile);
// System.out.println(f1.getPath());
File f2 = new File(comFile);
try {
boolean compare1and2 = FileUtils.contentEquals(f1, f2);
// Toast.makeText(getApplicationContext(), "Are " + uFiles[k] + " and " + comFile + " the same? " + compare1and2, Toast.LENGTH_SHORT).show();
if (!compare1and2) {
File f4 = new File(Environment.getExternalStorageDirectory() + "/upload/" + uFiles[k].getName());
File f3 = new File(comFile);
FileUtils.copyFile(f3, f4);
f1.delete();
Toast.makeText(getApplicationContext(), "File uploaded" , Toast.LENGTH_SHORT).show();
System.out.println("file uploaded");
} else {
f1.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
fileList.get(i).delete();
}
}
}
public ArrayList<File> getfile (File dir)
{
Log.e("logger", dir.toString());
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
fileList.add(listFile[i]);
getfile(listFile[i]);
} else {
if (listFile[i].getName().endsWith(".png")
|| listFile[i].getName().endsWith(".jpg")
|| listFile[i].getName().endsWith(".jpeg")
|| listFile[i].getName().endsWith(".gif"))
{
fileList.add(listFile[i]);
}
}
}
}
return fileList;
}
public IBinder onBind(Intent intent)
{
Toast.makeText(getApplicationContext()," onbind", Toast.LENGTH_SHORT).show();
return mBinder;
}
}
公共类MyService扩展服务
{
朱鹭;
私有文件根;
private ArrayList fileList=new ArrayList();
@凌驾
public void onCreate()
{
root=新文件(Environment.getExternalStorageDirectory()+“/user/”;
getfile(root);
对于(int i=0;i0){
for(int i=0;i
空指针异常
这是一个一般性问题,因为类的实例
未启动,您将收到此错误
当您使用第三方库
且不确定它在某些方法
中的行为时,应始终检查该方法
生成的给定实例
是否为空,在对该参考变量
采取任何操作之前,因为该方法
可能返回了null
在代码中输入如下条件
if(object != null){
// now you can call methods on this reference variable as it is not null
}else{
// forget it, the reference variable, does not hold address to any object, if you try anything with this, VM will complain with Null Pointer Exception
}
我建议您检查变量如下
root, listFile[], uFiles, f1, f2, f3, f4,
请注意,有些目录可能有子目录,但情况并不总是如此,所以必须检查<代码>列表文件()
is确实返回了文件数组,否则您必须在code
中处理更多的bug
,请共享日志,以便有人能为我的问题提供正确答案。您的问题是什么?在这个主题中,你建议你的帖子是关于一个空指针异常的,但你甚至不给它命名。或者说是哪条语句引起的。@Hema先分享logcat。