-2

我使用列表视图创建了一个文件资源管理器,可以让我们查看所有存储卡文件夹和文件。现在,当我单击任何文件时,会出现一个对话框,其中提供三个选项/按钮,即隐藏、发现和取消。现在我无法从列表视图中检索文件名并更改其名称,以便文件隐藏在画廊中。我正在上传我的代码,它给出了错误无法从活动执行方法。

Java 文件包含隐藏逻辑。 onHide()onUnhide()是隐藏和取消隐藏文件的方法

package com.example.settingspro;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.Environment;
import android.app.Dialog;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class pichide extends ListActivity {

    private List<String> item = null;
    private List<String> path = null;
    private String root;

    private TextView myPath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pichide);
        myPath = (TextView)findViewById(R.id.path);

        root = Environment.getExternalStorageDirectory().getPath();

        getDir(root);
    }

    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(root))
        {
            item.add(root);
            path.add(root);
            item.add("../");
            path.add(f.getParent());
        }

        for(int i=0; i < files.length; i++)
        {
            File file = files[i];

            if(!file.isHidden() && file.canRead()) {
                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.hidelistlayout, item);
        setListAdapter(fileList);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        // TODO Auto-generated method stub
        File file = new File(path.get(position));

        if (file.isDirectory())
        {
            if(file.canRead())
            {
                getDir(path.get(position));
            }
            else
            {
                Dialog settingsDialog = new Dialog(this);
                settingsDialog.setTitle("Options");
                settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.button_layout1, null));
                settingsDialog.show();
            }
        }
        else
        {
            Dialog settingsDialog = new Dialog(this);
            settingsDialog.setTitle("Options");
            settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.button_layout1, null));
            settingsDialog.show();
        }
    }
    public void onHide(View v)
    {
        long id;
        ListView lv = getListView();
        int position = lv.getPositionForView(v);

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

        if (file.isDirectory())
        {
            if(file.canRead())
            {
                getDir(path.get(position));
            }
            else
            {
                String s = file.getName();
                File f = new File(s);
                File f1 = new File(s + ".aaa");
                f.renameTo(f1);
                Toast.makeText(getApplicationContext(),
                "File Hidden", Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
            String s = file.getName();
            File f = new File(s);
            File f1 = new File(s + ".aaa");
            f.renameTo(f1);
            Toast.makeText(getApplicationContext(),
            "File Hidden", Toast.LENGTH_SHORT).show();
        }
    }

    public void onUnhide(View v)
    {
        long id;
        ListView lv = getListView();
        int position = lv.getPositionForView(v);
        File file = new File(path.get(position));

        if (file.isDirectory())
        {
            if(file.canRead())
            {
                getDir(path.get(position));
            }
            else
            {
                String s = file.getName();
                File f = new File(s);
                File f1 = new File(s);
                f.renameTo(f1);
                Toast.makeText(getApplicationContext(),
                "File UnHidden", Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
            String s = file.getName();
            File f = new File(s);
            File f1 = new File(s);
            f.renameTo(f1);
            Toast.makeText(getApplicationContext(),
            "File UnHidden", Toast.LENGTH_SHORT).show();
        }
    }

    public void onBack(View v)
    {
        finish();
    }
}

对话框Button_layout1.xml的LayoutFile

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back2"
    android:orientation="vertical" >

<Button
    android:id="@+id/hidebt"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="onHide"
    android:text="@string/hide"
    android:textColor="#ffffff"
    android:textStyle="bold" />

<Button
    android:id="@+id/unhidebt"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/hidebt"
    android:layout_below="@+id/hidebt"
    android:onClick="onUnhide"
    android:text="@string/unhide"
    android:textColor="#ffffff"
    android:textStyle="bold" />

<Button
    android:id="@+id/backbt"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/unhidebt"
    android:layout_below="@+id/unhidebt"
    android:onClick="onBack"
    android:text="@string/back"
    android:textColor="#ffffff"
    android:textStyle="bold" />

</RelativeLayout>

使用 listview 的 picide.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/status_bar_background22"
    android:orientation="vertical" >

<TextView
    android:id="@+id/path"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:textColor="#ffffff" 
    android:textStyle="bold" />
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<TextView
    android:id="@android:id/empty"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/nodata"
    android:textColor="#ffffff" 
    android:textStyle="bold"
    />

</LinearLayout>

我的 ListView 项目布局 hidelistlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowtext"
    android:layout_width="fill_parent"
    android:layout_height="30sp"
    android:textSize="25sp"
    android:textColor="#ffffff"
    android:textStyle="bold" />

日志:

08-29 19:14:18.572: D/AndroidRuntime(11136): Shutting down VM
08-29 19:14:18.572: W/dalvikvm(11136): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-29 19:14:18.602: E/AndroidRuntime(11136): FATAL EXCEPTION: main
08-29 19:14:18.602: E/AndroidRuntime(11136): java.lang.IllegalStateException: Could not execute method of the activity
08-29 19:14:18.602: E/AndroidRuntime(11136):    at android.view.View$1.onClick(View.java:3599)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at android.view.View.performClick(View.java:4204)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at android.view.View$PerformClick.run(View.java:17355)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at android.os.Handler.handleCallback(Handler.java:725)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at android.os.Looper.loop(Looper.java:137)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at android.app.ActivityThread.main(ActivityThread.java:5041)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at java.lang.reflect.Method.invokeNative(Native Method)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at java.lang.reflect.Method.invoke(Method.java:511)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at dalvik.system.NativeStart.main(Native Method)
08-29 19:14:18.602: E/AndroidRuntime(11136): Caused by: java.lang.reflect.InvocationTargetException
08-29 19:14:18.602: E/AndroidRuntime(11136):    at java.lang.reflect.Method.invokeNative(Native Method)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at java.lang.reflect.Method.invoke(Method.java:511)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at android.view.View$1.onClick(View.java:3594)
08-29 19:14:18.602: E/AndroidRuntime(11136):    ... 11 more
08-29 19:14:18.602: E/AndroidRuntime(11136): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
08-29 19:14:18.602: E/AndroidRuntime(11136):    at java.util.ArrayList.get(ArrayList.java:306)
08-29 19:14:18.602: E/AndroidRuntime(11136):    at com.example.settingspro.pichide.onHide(pichide.java:124)
08-29 19:14:18.602: E/AndroidRuntime(11136):    ... 14 more
08-29 19:14:18.632: W/ActivityManager(315):   Force finishing activity com.example.settingspro/.pichide
08-29 19:14:18.642: W/WindowManager(315): Failure taking screenshot for (123x221) to layer 21020
4

1 回答 1

0

您应该始终阅读比第一行更多的堆栈跟踪行。这是你的罪魁祸首:

 08-29 19:14:18.602: E/AndroidRuntime(11136): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
 08-29 19:14:18.602: E/AndroidRuntime(11136):   at java.util.ArrayList.get(ArrayList.java:306)
 08-29 19:14:18.602: E/AndroidRuntime(11136):   at com.example.settingspro.pichide.onHide(pichide.java:124)
 08-29 19:14:18.602: E/AndroidRuntime(11136):   ... 14 more

所以看看(顺便说一句:你使用错误的命名约定)的第124pichide.java并修复:)

于 2013-08-29T19:39:59.657 回答