Android Studio新手 - 如何读取文件并输出?

时间:2017-10-10 18:28:53

标签: android

我已经在这个项目上工作了几天,我无法弄清楚它为什么会一直崩溃。该项目是获取一个位于assets文件夹中的文件,其中包含一堆双打(使用包含文件名的用户的输入),找到它们的余弦,并显示结果供用户查看。这是我的MainActivity类:

package com.example.brandon.lab1;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.os.StrictMode;
import android.content.res.AssetManager;
import java.util.Scanner;
import java.util.ArrayList;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.ThreadPolicy policy = new               StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }


    return super.onOptionsItemSelected(item);
}


public void cosAndDisplay(View view)
        throws IOException {
    EditText et;
    TextView tv;
    int numOfItems;
    String fileName;
    tv = (TextView) findViewById(R.id.text_main);
    et = (EditText) findViewById(R.id.edit_infile);

    fileName = (et.getText().toString());
    AssetManager assetManager = getAssets();
    Scanner fsc = new Scanner(assetManager.open(fileName));
    ArrayList<Double> nums = new ArrayList<>();
    while (fsc.hasNextDouble()) {
        String[] line = fsc.nextLine().split(" ");
        for (String s : line) {
            nums.add(Double.parseDouble(s));
        }
    }
    fsc.close();
    numOfItems = nums.size();
    cos_it(nums, numOfItems);
    ArrayList<String> numList = new ArrayList<>();
    for (int x = 0; x < numOfItems; x++) {
        numList.add(nums.get(x).toString());
    }

    NumberFormat formatter = new DecimalFormat("#0.00");
    for (int y = 0; y < numOfItems; y++) {
        tv.setText(formatter.format(numList.get(y)));
    }
}



public static void cos_it(ArrayList<Double> a, int num_items) {
    for(int x=0; x<=num_items; x++) {
        a.set(x, Math.cos(a.get(x)));
    }
}

}

这是崩溃报告:

--------- beginning of crash
10-10 16:02:25.649 3009-3009/com.example.brandon.lab1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.brandon.lab1, PID: 3009
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
    at android.view.View.performClick(View.java:4780)
    at android.view.View$PerformClick.run(View.java:19866)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
    at android.view.View.performClick(View.java:4780) 
    at android.view.View$PerformClick.run(View.java:19866) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
 Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
    at java.util.ArrayList.get(ArrayList.java:308)
    at com.example.brandon.lab1.MainActivity.cos_it(MainActivity.java:104)
    at com.example.brandon.lab1.MainActivity.cosAndDisplay(MainActivity.java:88)
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
    at android.view.View.performClick(View.java:4780) 
    at android.view.View$PerformClick.run(View.java:19866) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
10-10 16:02:28.687 3009-3009/? I/Process: Sending signal. PID: 3009 SIG: 9

1 个答案:

答案 0 :(得分:0)

如logcat中所指定的那样,你得到了一个

  

引起:java.lang.IndexOutOfBoundsException:索引0无效,大小为0

cos_it 方法中,其中ArrayList的输入参数大小为0,这意味着它不包含任何项目。

如果正确填充了所需的ArrayList,则需要进行调试。