当前位置: 代码迷 >> Android >> 点击搜寻,崩溃了
  详细解决方案

点击搜寻,崩溃了

热度:7   发布时间:2016-04-28 00:41:37.0
点击搜索,崩溃了
想做一个搜索Android文件的功能,主要功能已经贴出,输入文字后点击就直接崩溃
private File file=new File("/sdcard");
private String path;
private String info;
private String theKey_formInput;
private TextView show_Result;
private EditText input_SearchKey_Edit;
private Button toSearch_Button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findSD();
    }
    
    //SD卡文件搜索
    protected void findSD() {
setContentView(R.layout.sdcard);
show_Result=(TextView)findViewById(R.id.TextView_Result);
input_SearchKey_Edit=(EditText)findViewById(R.id.input_Key_EditText);
toSearch_Button=(Button)findViewById(R.id.Button_Search);
info=getString(R.string.info);
toSearch_Button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
path="";
show_Result.setText("");
theKey_formInput=input_SearchKey_Edit.getText().toString();
BrowserFile(file);
}
});
}

public void BrowserFile(File file)
{
if(theKey_formInput.equals(""))
{
Toast.makeText(this, getString(R.string.pleaseInput), Toast.LENGTH_SHORT).show();
}else
{
ToSearchFiles(file);
if(show_Result.getText().equals(""))
{
Toast.makeText(this, getString(R.string.noFound), Toast.LENGTH_SHORT).show();
}
}
}

public void ToSearchFiles(File file)
{
File[] the_Files=file.listFiles();
for(File file2:the_Files)
{
if(file2.isDirectory())
{
ToSearchFiles(file2);
}
else
{
try {
if(file2.getName().indexOf(theKey_formInput)>-1)
{
path+="\n"+file2.getPath();
show_Result.setText(info+path);
}
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, getString(R.string.pathError), Toast.LENGTH_SHORT).show();
}
}
}
}


------解决思路----------------------
引用:
Quote: 引用:

private File file=new File("/sdcard");

 File[] the_Files=file.listFiles();
debug这,你就会发现,是哪的问题。
这是debug时获取的数据

会不会是哪个目录没有权限,所以不能遍历,得不到文件。
------解决思路----------------------
引用:
Quote: 引用:

那你sd卡的读写的权限加了吗?
手机有可能,但模拟器会吗?

需要.
  相关解决方案