2013-08-29 74 views
-2

我創建了一個使用列表視圖的文件資源管理器,可以讓我們查看所有存儲卡文件夾和文件。現在當我點擊任何文件時,出現一個對話框,提供三個選項/按鈕,隱藏,揭開並取消。現在我無法從列表視圖中檢索文件名並更改其名稱,以便文件在gallery中隱藏。我正在上傳我的代碼,並且它給出錯誤無法從活動執行方法。隱藏在Android中的文件

包含隱藏邏輯的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(); 
    } 
} 

對話框的LayoutFile框Button_layout1.xml

<?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> 

pichide.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> 

我的列表視圖項目佈局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 
+0

你能告訴您的日誌一些錯誤行。 – Rekha

+0

已將日誌貓添加到帖子 –

+1

閱讀您的例外情況。你有一個'IndexOutOfBounds'因爲你試圖訪問元素'-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 

所以看線的pichide.java (順便說一句:你用錯誤的命名約定)和修復:)

+0

Thanx ...將檢查並告訴如果仍然發生錯誤 –

+0

我在這行中得到錯誤可能是錯誤的文件file = new File(path.get (位置)); –