Java 为什么要跑步;arraylist.get();在Android Studio中崩溃我的应用程序?

Java 为什么要跑步;arraylist.get();在Android Studio中崩溃我的应用程序?,java,android,eclipse,file,arraylist,Java,Android,Eclipse,File,Arraylist,我试图在Android Studio中读取一个文件并将每个字符串放入arraylist,但当我试图从arraylist获取字符串时,应用程序崩溃,并显示以下消息: 消息:“很遗憾,应用程序已停止” 谁能告诉我怎么了 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ///

我试图在Android Studio中读取一个文件并将每个字符串放入arraylist,但当我试图从arraylist获取字符串时,应用程序崩溃,并显示以下消息:

消息:“很遗憾,应用程序已停止”

谁能告诉我怎么了

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


////////////////////////////////////////////////////////////////////////////
    String text = "";

    tv_view = (TextView) findViewById(R.id.textview1);

    Scanner s = new Scanner(System.in);

    File n = new File("C:\\Users\\Admin\\AndroidStudioProjects\\LOTOS.1\\app\\src\\main\\assets\\nouns.txt");

    //Instantiate Scanner s with f variable within parameters
    //surround with try and catch to see whether the file was read or not
    try {
        s = new Scanner(n);
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }

    //Instantiate a new ArrayList of String type
    ArrayList<String> theWord = new ArrayList<String>();
   

    //while it has next ..
    while(s.hasNext()){
        //Initialise str with word read
        String str=s.next();

        //add to ArrayList
        theWord.add(str);

    }

    text = theWord.get(150);

    tv_view.setText(text);
    //return ArrayList


}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
////////////////////////////////////////////////////////////////////////////
字符串文本=”;
tv_view=(TextView)findViewById(R.id.textview1);
扫描仪s=新的扫描仪(System.in);
文件n=新文件(“C:\\Users\\Admin\\AndroidStudioProjects\\LOTOS.1\\app\\src\\main\\assets\\nowns.txt”);
//用参数内的f变量实例化扫描仪s
//用try-and-catch环绕以查看文件是否已读取
试一试{
s=新扫描仪(n);
}catch(filenotfounde异常){
e、 printStackTrace();
}
//实例化字符串类型的新ArrayList
ArrayList theWord=newarraylist();
//当它有下一个。。
而(s.hasNext()){
//用单词read初始化str
字符串str=s.next();
//添加到ArrayList
添加(str);
}
text=单词.get(150);
tv_view.setText(文本);
//返回数组列表
}

问题是,在Android系统中,您无法直接从windows系统读取文件。Android设备和windows是完全不同的系统。即使您已将文件放在
assets
文件夹中,也无法读取,因为路径指向windows结构

您的Android设备或模拟器无法读取以下路径:
C:\\Users\\Admin…

要从Android Studio中的
assests
访问文件,应使用
getAssets().open(…)
方法

下面是如何读取文件的示例

BufferedReader reader = null;
reader = new BufferedReader(
        new InputStreamReader(getAssets().open("nouns.txt"), "UTF-8")); 

// do reading, usually loop until end of file reading 
String mLine;
while ((mLine = reader.readLine()) != null) {
       //process 
       ...
}

它在室内运行良好Eclipse@ShaishavJogani问题是它编译得很好,事件日志中没有错误。但是应用程序崩溃如果它崩溃了,肯定是有错误的。谢谢,我会尝试一下,但是因为我的声誉不到15,所以它不会公开显示,不管它是如何记录的。非常感谢againIt进行了一些调整,非常感谢您的时间和建议