尝试从listview中删除空指针异常

时间:2012-11-05 04:15:00

标签: android android-listview nullpointerexception

我在deleteIt

filelist.notifyDataSetChanged();方法中得到空指针异常

我需要一些帮助尝试删除该文件,请善待。我花了几个小时来添加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();
}

}

2 个答案:

答案 0 :(得分:0)

试试这个改变你的陈述

ArrayAdapter filelist = null;

ArrayAdapter<String> filelist = null;

并且您还在getDir方法中重新声明文件列表,因此将其更改为

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

答案 1 :(得分:0)

 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();

    }

}