《Google Android SDK开发范例大全》第四章【4.21查找根目录下所有文件】这个例子里面,window下eclipse环境下运行时,如果目录内有用中文命名的文件或者文件夹就会出错,请问这是为什么,有没有解决办法?
报的错为01-19 13:16:14.112: A/libc(1065): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)
才开始学习编程,请稍微解答详细和浅显一点,谢谢!package irdc.ex04_21;
 
import java.io.File;
 
import java.util.ArrayList;
 
import java.util.List;
 
import android.app.AlertDialog;
 
import android.app.ListActivity;
 
import android.content.DialogInterface;
 
import android.content.Intent;
 
import android.os.Bundle;
 
import android.view.View;
 
import android.view.View.OnClickListener;
 
import android.widget.ArrayAdapter;
 
import android.widget.ListView;
 
import android.widget.TextView;
 
import android.widget.Button;
 public class EX04_21 extends ListActivity
 
{
   private List<String> items=null;
 
  private List<String> paths=null;
 
  private String rootPath="/data/data/irdc.ex04_21";
 
  Button leftBtn;
 
  
 
  /** Called when the activity is first created. */
 
  @Override
 
  protected void onCreate(Bundle icicle)
 
  {
 
    super.onCreate(icicle);
 
    setContentView(R.layout.main);
 
    getFileDir(rootPath);    
    leftBtn=(Button)findViewById(R.id.button1);
 
    leftBtn.setOnClickListener(leftBtnListener);
 
  }
 
  
 
  private OnClickListener leftBtnListener = new OnClickListener(){
 
      public void onClick(View v)
 
      {
 
        leftBtn.setText("<Back");
 
        getFileDir(rootPath);
 
      }
 
    };
 
  
 
  private void getFileDir(String filePath)
 
  {
 
    items=new ArrayList<String>();
 
    paths=new ArrayList<String>();  
 
    File f=new File(filePath);  
 
    File[] files=f.listFiles();
     for(int i=0;i<files.length;i++)
 
    {
 
      File file=files[i];
 
      items.add(file.getName());
 
      paths.add(file.getPath());
 
    }
 
    
    ArrayAdapter<String> fileList = 
       new ArrayAdapter<String>(this,R.layout.file_row, items);
 
    setListAdapter(fileList);
 
  }
   @Override
 
  protected void onListItemClick(ListView l,View v,int position,long id)
 
  {
 
    File file = new File(paths.get(position));
 
    if(file.canRead())
 
    {
 
      if (file.isDirectory())
 
      {
 
        getFileDir(paths.get(position));
 
      }
 
      else
 
      {
 
        new AlertDialog.Builder(this)
 
            .setTitle("Message")
 
            .setMessage("["+file.getName()+"] is File!")
 
            .setPositiveButton("OK",
 
              new DialogInterface.OnClickListener()
 
              {
 
                public void onClick(DialogInterface dialog,int which)
 
                {
 
                }
 
              }).show();         
 
      }
 
    }
 
    else
 
    {
 
      new AlertDialog.Builder(this)
 
          .setTitle("Message")
 
          .setMessage("权限不足!")
 
          .setPositiveButton("OK",
 
            new DialogInterface.OnClickListener()
 
            {
 
              public void onClick(DialogInterface dialog,int which)
 
              {
 
              }
 
            }).show();     
 
    }
 
  }
 
}

解决方案 »

  1.   

    01-19 14:33:39.563: W/dalvikvm(566): JNI WARNING: input is not valid Modified UTF-8: illegal continuation byte 0xd0
     01-19 14:33:39.563: W/dalvikvm(566):              string: '???????'
     01-19 14:33:39.563: W/dalvikvm(566):              in Ljava/io/File;.listImpl:(Ljava/lang/String;)[Ljava/lang/String; (NewStringUTF)
     01-19 14:33:39.573: I/dalvikvm(566): "main" prio=5 tid=1 NATIVE
     01-19 14:33:39.573: I/dalvikvm(566):   | group="main" sCount=0 dsCount=0 obj=0x40997460 self=0x12810
     01-19 14:33:39.573: I/dalvikvm(566):   | sysTid=566 nice=0 sched=0/0 cgrp=default handle=1074082952
     01-19 14:33:39.573: I/dalvikvm(566):   | schedstat=( 334860174 892884911 75 ) utm=13 stm=20 core=0
     01-19 14:33:39.573: I/dalvikvm(566):   at java.io.File.listImpl(Native Method)
     01-19 14:33:39.573: I/dalvikvm(566):   at java.io.File.list(File.java:749)
     01-19 14:33:39.573: I/dalvikvm(566):   at java.io.File.listFiles(File.java:791)
     01-19 14:33:39.573: I/dalvikvm(566):   at irdc.ex04_21.EX04_21.getFileDir(EX04_21.java:63)
     01-19 14:33:39.583: I/dalvikvm(566):   at irdc.ex04_21.EX04_21.onCreate(EX04_21.java:40)
     01-19 14:33:39.583: I/dalvikvm(566):   at android.app.Activity.performCreate(Activity.java:4465)
     01-19 14:33:39.593: I/dalvikvm(566):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
     01-19 14:33:39.593: I/dalvikvm(566):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
     01-19 14:33:39.593: I/dalvikvm(566):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
     01-19 14:33:39.593: I/dalvikvm(566):   at android.app.ActivityThread.access$600(ActivityThread.java:122)
     01-19 14:33:39.593: I/dalvikvm(566):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
     01-19 14:33:39.593: I/dalvikvm(566):   at android.os.Handler.dispatchMessage(Handler.java:99)
     01-19 14:33:39.593: I/dalvikvm(566):   at android.os.Looper.loop(Looper.java:137)
     01-19 14:33:39.593: I/dalvikvm(566):   at android.app.ActivityThread.main(ActivityThread.java:4340)
     01-19 14:33:39.593: I/dalvikvm(566):   at java.lang.reflect.Method.invokeNative(Native Method)
     01-19 14:33:39.593: I/dalvikvm(566):   at java.lang.reflect.Method.invoke(Method.java:511)
     01-19 14:33:39.593: I/dalvikvm(566):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
     01-19 14:33:39.593: I/dalvikvm(566):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
     01-19 14:33:39.593: I/dalvikvm(566):   at dalvik.system.NativeStart.main(Native Method)
     01-19 14:33:39.603: E/dalvikvm(566): VM aborting
     01-19 14:33:39.603: A/libc(566): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)
      

  2.   

    在网上找了一个可能的办法:http://blog.csdn.net/andytsui/article/details/6161165
     
    里面说要改checkjni.c文件,请问checkjni.c在哪里???谢谢
      

  3.   

    workspace里面的编码也改成UTF-8了,还是不行。
    请问该怎么办,求解!
      

  4.   

    你是要读取中文文件名的文件是么?
    文件名你是可以获取的,直接转换成gbk编码就可以在程序里正常显示了。
    String filename = new String(path, "GB2312");//path是带中文的路径或中文文件名
    转换后的filename就是可以正常显示的了。
    你改workspace的编码没用的,那只是ide的编码,和程序没关系的。
      

  5.   

    谢谢dinjay的解答,感觉有希望了!
    可是我基础差,刚才加了你说的代码:
    private String midPath="/data/data/irdc.ex04_21/新建文件";
    private String rootPath=new String(midPath,"gb2312 ");报错:The constructor String(String, String) is undefined
    需要怎么改?谢谢!
      

  6.   

    改成这种也不行:
      private String midPath="/data/data/irdc.ex04_21/新建文件";
      private String rootPath=new String(midPath.getBytes("UTF8"),"gb2312");报错:Default constructor cannot handle exception type UnsupportedEncodingException thrown by implicit super constructor. Must define an explicit constructor
      

  7.   

    我的需求是这样的:
    读取某一个目录下面的所有文件(包括英文文件名的文件和中文文件名的文件)显示在一个ListActivity里面。
      

  8.   

    If you’re using the emulator, CheckJNI is on by default.If you have a rooted device, you can use the following sequence of commands to restart the runtime with CheckJNI enabled:adb shell stop
    adb shell setprop dalvik.vm.checkjni true
    adb shell startIn either of these cases, you’ll see something like this in your logcat output when the runtime starts:D AndroidRuntime: CheckJNI is ON
    If you have a regular device, you can use the following command:adb shell setprop debug.checkjni 1This won’t affect already-running apps, but any app launched from that point on will have CheckJNI enabled. (Change the property to any other value or simply rebooting will disable CheckJNI again.) In this case, you’ll see something like this in your logcat output the next time an app starts:D Late-enabling CheckJNI