因此,对于我的代码,我要做的是在包含整数的.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);
}
}
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>
答案 0 :(得分:-2)
在您的特定情况下,您应该将data.txt放在 src / main / assets 文件夹中。然后代替行
InputStream inputStream = openFileInput(FILENAME);
使用以下行
InputStream inputStream = getAssets().open(FILENAME);