Android Java-读取整数的文本文件,将整数存储在数组中,并绘制它们的图形

Android Java-读取整数的文本文件,将整数存储在数组中,并绘制它们的图形,java,android,arrays,Java,Android,Arrays,因此,对于我的代码,我尝试读取包含整数的.txt文件,通过parseInt()将这些整数存储到数组列表中,然后通过GraphView绘制这些点。我已经对我的代码进行了一些调试,我注意到没有一个点被绘制成图形,当我通过标签打印数组列表的值来测试代码时,它会使我的应用程序崩溃。我不确定正在发生什么,因为我认为我在这个过程中走的是正确的道路。下面是我的代码*注意:我使用TextView标签作为测试来确定数据是否被实际读取(在本例中不是)。提前谢谢 MainActivity.java package c

因此,对于我的代码,我尝试读取包含整数的.txt文件,通过parseInt()将这些整数存储到数组列表中,然后通过GraphView绘制这些点。我已经对我的代码进行了一些调试,我注意到没有一个点被绘制成图形,当我通过标签打印数组列表的值来测试代码时,它会使我的应用程序崩溃。我不确定正在发生什么,因为我认为我在这个过程中走的是正确的道路。下面是我的代码*注意:我使用TextView标签作为测试来确定数据是否被实际读取(在本例中不是)。提前谢谢

MainActivity.java

package com.example.anjanarajagopal.graph;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {

    private static final String TAG = MainActivity.class.getName();
    private static final String FILENAME = "data.txt";
    private TextView textview1;
    private TextView textview2;
    private TextView textview3;
    private TextView textview4;
    private TextView textview5;

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

        List<Integer> data = new ArrayList<Integer>();
        try
        {
            InputStream inputStream = openFileInput(FILENAME);
            if(inputStream != null){
                InputStreamReader streamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(streamReader);
                String line;
                while((line = bufferedReader.readLine()) != null)
                {
                    int i = Integer.parseInt(line);
                    data.add(i);
                }
            }
            inputStream.close();
        }
        catch(Exception e)
        {
            System.err.format("Exception occurred trying to read '%s'.", FILENAME);
            e.printStackTrace();
            //return null;
        }
        //for(int i = 0; i < data.size(); i++)
        //{
        //    int value = data.get(i);
        //    System.out.print(value + " ");
        //}
        //textview1 = (TextView)findViewById(R.id.textView1);
        //textview2 = (TextView)findViewById(R.id.textView2);
        //textview3 = (TextView)findViewById(R.id.textView3);
        //textview4 = (TextView)findViewById(R.id.textView4);
        //textview5 = (TextView)findViewById(R.id.textView5);
        //textview1.setText(Integer.toString(data.get(0)));
        //textview2.setText(Integer.toString(data.get(1)));
        //textview3.setText(Integer.toString(data.get(2)));
        //textview4.setText(Integer.toString(data.get(3)));
        //textview5.setText(Integer.toString(data.get(4)));

        GraphView graph;
        graph = (GraphView) findViewById(R.id.graph);
        for(int i = 0; i < data.size(); i++) {
            LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{
                    new DataPoint(i, data.get(i))
            });
            graph.addSeries(series);
        }
}
@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);
    }

}
package com.example.anjanarajagopal.graph;
导入android.os.Bundle;
导入android.support.v7.app.ActionBarActivity;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.TextView;
导入com.jjoe64.graphview.graphview;
导入com.jjoe64.graphview.series.DataPoint;
导入com.jjoe64.graphview.series.LineGraphSeries;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
公共类MainActivity扩展了ActionBarActivity{
私有静态最终字符串标记=MainActivity.class.getName();
私有静态最终字符串FILENAME=“data.txt”;
私有文本视图文本视图1;
私有文本视图文本视图2;
私有文本视图文本视图3;
私有文本视图文本视图4;
私有文本视图文本视图5;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
列表数据=新的ArrayList();
尝试
{
InputStream InputStream=openFileInput(文件名);
如果(inputStream!=null){
InputStreamReader streamReader=新的InputStreamReader(inputStream);
BufferedReader BufferedReader=新的BufferedReader(streamReader);
弦线;
而((line=bufferedReader.readLine())!=null)
{
int i=整数.parseInt(行);
数据.添加(i);
}
}
inputStream.close();
}
捕获(例外e)
{
System.err.format(“尝试读取'%s',文件名时发生异常”);
e、 printStackTrace();
//返回null;
}
//对于(int i=0;i
data.txt 142 149 146 142 152 165 178 188 171 169 179 155 129 117 125 155 204 251 270 265

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView5"
    android:layout_below="@+id/textView4"
    android:layout_alignLeft="@+id/textView3"
    android:layout_alignStart="@+id/textView3" />
<com.jjoe64.graphview.GraphView
    android:layout_width="match_parent"
    android:layout_height="200dip"
    android:id="@+id/graph"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView3"
    android:layout_below="@+id/textView2"
    android:layout_alignRight="@+id/graph"
    android:layout_alignEnd="@+id/graph" />

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView4"
    android:layout_below="@+id/textView3"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignStart="@+id/textView2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello world!"
    android:id="@+id/textView1"
    android:layout_below="@+id/textView5"
    android:layout_alignRight="@+id/textView4"
    android:layout_alignEnd="@+id/textView4" />
</RelativeLayout>

在您的特定情况下,您应该将data.txt放在src/main/assets文件夹中。然后就不用排队了

InputStream inputStream = openFileInput(FILENAME);
使用下面的行

InputStream inputStream = getAssets().open(FILENAME);

一点建议:使用数据库而不是文本文件来存储数据。这篇文章应该是评论,而不是答案。我没有足够的声誉来发表评论。这不是发布非答案的有效理由。文件每行一个整数。好的,控制台上有stacktrace吗?