"不幸的是,MyApp已经停止了#34; android

时间:2016-02-18 13:12:36

标签: android listview view

我正在尝试查看目录中的项目的代码,但每次单击代码来自的按钮时,应用程序都会关闭,并向我显示错误说“#34;不幸的是,YourApp已停止了。",我不知道为什么会这样。

以下是我尝试的代码:

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Environment;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ViewTextFiles 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.viewtextfiles);
    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.row, 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{
            new AlertDialog.Builder(this)
                    .setIcon(R.drawable.ic_launcher)
                    .setTitle("[" + file.getName() + "] folder can't be read!")
                    .setPositiveButton("OK", null).show();
        }
    }else {
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("[" + file.getName() + "]")
                .setPositiveButton("OK", null).show();

    }
}

PS我在真实设备中尝试代码但没有使用USB线缆(我从我的设备中导出我的.apk),因为我的设备出现了问题,而我的设备没有问题。当我的设备通过USB线连接时识别我的设备,和模拟器一样,它的处理器有问题,我不知道如何解决。

1 个答案:

答案 0 :(得分:0)

您必须在android清单文件中提供此权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

您的Activity类应如下所示:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

TextView myPath;
private List<String> item = null;
private List<String> path = null;
private String root;
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myPath = (TextView)findViewById(R.id.txt);
    list = (ListView) findViewById(R.id.listView);
    list.setOnItemClickListener(this);

    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, android.R.layout.simple_list_item_1, item);
    setListAdapter(fileList);
}

private void setListAdapter(ArrayAdapter<String> adapter) {
    list.setAdapter(adapter);
}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    File file = new File(path.get(position));

    if (file.isDirectory())
    {
        if(file.canRead()){
            getDir(path.get(position));
        }else{
            new AlertDialog.Builder(this)
                    .setTitle("[" + file.getName() + "] folder can't be read!")
                    .setPositiveButton("OK", null).show();
        }
    }else {
        new AlertDialog.Builder(this)
                .setTitle("[" + file.getName() + "]")
                .setPositiveButton("OK", null).show();

    }
}

}

您的activity_main.xml文件应如下所示:

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

<TextView
    android:id="@+id/txt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listView" />
</LinearLayout>
相关问题