Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.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中的文件隐藏_Android - Fatal编程技术网

Android中的文件隐藏

Android中的文件隐藏,android,Android,我使用列表视图创建了一个文件浏览器,可以查看所有存储卡文件夹和文件。现在,当我单击任何文件时,会出现一个对话框,其中提供了三个选项/按钮,即隐藏、发现和取消。现在,我在从列表视图检索文件名并更改其名称以使文件隐藏在库中时遇到问题。我正在上载代码,它给出了一个错误,即无法从活动中执行方法 包含隐藏逻辑的Java文件。 onHide和onUnhide是隐藏和取消隐藏文件的方法 package com.example.settingspro; import java.io.File; import

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

包含隐藏逻辑的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();
    }
}

您应该总是比第一行读更多的stacktrace行。这是你的罪魁祸首:

 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

因此,请看pichide.java btw的第124行:您使用了错误的命名约定并修复:

您能否显示日志中的一些错误行。将日志cat添加到postRead您的异常中。您有一个IndexOutOfBounds,因为您尝试访问元素-1。Thank将看到错误并告诉您是否发生了任何其他错误:我在此行中遇到错误,可能是什么错误File File=new Filepath.getposition;
<?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>
<?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
 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