Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 试图从listview中删除的空指针异常_Android_Android Listview_Nullpointerexception - Fatal编程技术网

Android 试图从listview中删除的空指针异常

Android 试图从listview中删除的空指针异常,android,android-listview,nullpointerexception,Android,Android Listview,Nullpointerexception,我在文件列表中的deleteIt方法中得到一个空指针异常 我需要一些帮助,试图删除该文件,请友好。如何添加longclick方法花了我几个小时。这可能会被人使用,但我需要帮助从列表中删除项目 public class AndroidExplorer extends ListActivity { private List<String> item = null; private List<String> path = null; private TextView myP

我在
文件列表中的
deleteIt
方法中得到一个空指针异常

我需要一些帮助,试图删除该文件,请友好。如何添加
longclick
方法花了我几个小时。这可能会被人使用,但我需要帮助从列表中删除项目

public class AndroidExplorer extends ListActivity 
{

private List<String> item = null;
private List<String> path = null;
private TextView myPath;

ArrayAdapter filelist = null;

File myFile = new   File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/GV-Skynet");
String root2 = myFile.getAbsolutePath();;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myPath = (TextView)findViewById(R.id.path);
    getDir(root2);


    ListView list = getListView();
    list.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) 
        {
            File file = new File(path.get(position));

            deleteIt("Do you want to Delete", file.getName(), file );
            return true;
        }
    });

}

private void showToast(String msg) 
{
    Toast error = Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG);
    error.show();
}

private void getDir(String dirPath)
{
    myPath.setText("Location: " + dirPath);

    item = new ArrayList<String>();
    path = new ArrayList<String>();

    File f = new File(dirPath);
    File[] files = f.listFiles();

    if(!dirPath.equals(root2))
    {
        item.add("Return to GV-Skynet Directory");
        path.add(f.getParent());
    }

    for(int i=0; i < files.length; i++)
    {
        File file = files[i];
        path.add(file.getPath());
        if(file.isDirectory())
            item.add(file.getName() + "/");
        else
            item.add(file.getName());
    }

    ArrayAdapter<String> fileList =
        new ArrayAdapter<String>(this, R.layout.row, item);
    setListAdapter(fileList);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{

    File file = new File(path.get(position));

    if (file.isDirectory())
    {
        if(file.canRead())
            getDir(path.get(position));
        else
        {
            new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle("[" + file.getName() + "] folder can't be read!")
            .setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                }
            }).show();
        }
    }
    else
    {

        String fileName = file.getName();
        String fname="";
        String ext="";
        int mid= fileName.lastIndexOf(".");
        fname=fileName.substring(0,mid);
        ext=fileName.substring(mid+1,fileName.length());



        if(ext.equals("jpg"))
        {
            Intent intent = new Intent("android.intent.action.VIEW");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);

            Uri uri = Uri.fromFile(file);
            intent.setDataAndType (uri, "image/*");
            this.startActivity(intent); 
        }

        if(ext.equals("3gp"))
        {
            Intent intent = new Intent("android.intent.action.VIEW");
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra ("oneshot", 0);
            intent.putExtra ("configchange", 0);
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType (uri, "video/*");
            this.startActivity(intent);
        }

    }
}

public void deleteIt( String title, String name, File file )
{
    final String name1 = name;
    final File tmFile = file;
    new AlertDialog.Builder(this)
    .setTitle(title)
    .setMessage(name1)
    .setPositiveButton("YES", new DialogInterface.OnClickListener() // OnClickListener()
    {
        public void onClick(DialogInterface arg0, int arg1) 
        {
            //item = new ArrayList<String>();
            item.remove(tmFile.getName()); // remove the item
            filelist.notifyDataSetChanged(); // let the adapter know to update

            showToast("File Deleted: " + tmFile.getName() );
        }
    })
    .setNegativeButton("NO", new DialogInterface.OnClickListener() // OnClickListener()
    {
        public void onClick(DialogInterface arg0, int arg1) {
            // do stuff onclick of CANCEL
            showToast("CANCELLING DELETE");
        }
    }).show();
}

}
公共类AndroidExplorer扩展了ListActivity
{
私有列表项=null;
私有列表路径=null;
私有文本视图myPath;
ArrayAdapter文件列表=空;
File myFile=新文件(Environment.getExternalStorageDirectory().getAbsolutePath()+“/GV Skynet”);
字符串root2=myFile.getAbsolutePath();;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPath=(TextView)findViewById(R.id.path);
getDir(root2);
ListView list=getListView();
list.setOnItemLongClickListener(新的OnItemLongClickListener(){
@凌驾
长单击(AdapterView父视图、视图、,
内部位置,长id)
{
File File=新文件(path.get(position));
deleteIt(“是否要删除”,file.getName(),file);
返回true;
}
});
}
私有void showtoos(字符串msg)
{
Toast error=Toast.makeText(getBaseContext(),msg,Toast.LENGTH_LONG);
error.show();
}
私有void getDir(字符串dirPath)
{
setText(“位置:“+dirPath”);
item=newarraylist();
路径=新的ArrayList();
文件f=新文件(dirPath);
File[]files=f.listFiles();
如果(!dirPath.equals(root2))
{
添加(“返回GV天网目录”);
add(f.getParent());
}
对于(int i=0;i
试试这个改变你的陈述

ArrayAdapter filelist = null;

ArrayAdapter filelist=null;
而且您正在getDir方法中重新声明filelist,所以将其更改为

filelist = new ArrayAdapter<String>(this,R.layout.row, item);
    setListAdapter(filelist);
filelist=newarrayadapter(此,R.layout.row,项);
setListAdapter(文件列表);
长点击(AdapterView父项、视图、int位置、长id){
File File=新文件(path.get(position));
if(file.exists()){
deleteIt(“是否要删除”,file.getName(),file);
}否则{
Toast.makeText(此“文件不存在”,Toast.LENGTH_SHORT).show();
}
}

您能否编辑您的帖子以提供LogCat错误输出?是否确实要在getDir()中声明局部变量ArrayAdapter文件列表?它覆盖类中声明的“ArrayAdapter文件列表”。
filelist = new ArrayAdapter<String>(this,R.layout.row, item);
    setListAdapter(filelist);
 onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    File file = new File(path.get(position));
    if (file.exists()) {
        deleteIt("Do you want to Delete", file.getName(), file);
    } else {
        Toast.makeText(this, "File not Exist", Toast.LENGTH_SHORT).show();

    }

}