为什么值顺序正确时出现java.lang.IllegalArgumentException错误?
Why does java.lang.IllegalArgumentException error appear when order of values is correct?
我是一名 A-level 计算机科学二年级的学生。我有一个课程作业项目,我决定使用 Android Studio 制作一个健身应用程序,但我遇到了一个我无法解决的错误,也不明白为什么会这样。该错误是由(activity)中的代码引起的。我根据 Logcat(这是代码 dataseries.resetData(grabData());
)对该行进行了评论。它位于 public void insertData() {
.
我得到的错误是 java.lang.IllegalArgumentException: The order of the values is not correct. X-Values have to be ordered ASC. First the lowest x value and at least the highest x value.
错误的完整详细信息如下所示。
我使用了 YouTube 上的一个教程来帮助我制作图表视图(因为我想制作一个在 y 轴和日期 x 轴上具有用户输入的图表)。我也完成了视频中所说的一切。 This is the video我用过
我不知道如何解决这个问题,所以到目前为止我唯一做的就是重做代码以确保我没有做错任何事情。但是,我仍然收到错误。我已经尝试联系制作视频的人,但他没有回应,而且可能不会,因为他最近没有在他的社交媒体上上传。
这是图形和用户输入的代码 (activity wTrackingTestingActivity
):
package com.example.fitmania;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.jjoe64.graphview.DefaultLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import java.text.SimpleDateFormat;
import java.util.Date;
public class wTrackingTestingActivity extends AppCompatActivity {
Button insertButton;
EditText inputTextY;
GraphView graphView;
DatabaseHandler dbh;
SQLiteDatabase sqLiteDatabase;
LineGraphSeries<DataPoint> dataseries = new LineGraphSeries<>(new DataPoint[0]);
@SuppressLint("SimpleDateFormat")
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_w_tracking_testing);
insertButton = (Button) findViewById(R.id.insertButton);
inputTextY = (EditText) findViewById(R.id.inputTextY);
graphView = (GraphView) findViewById(R.id.graph);
dbh = new DatabaseHandler(this);
sqLiteDatabase = dbh.getWritableDatabase();
graphView.addSeries(dataseries);
insertData();
}
public void insertData() {
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//set data and add to the database
//x value == date
long xValue = new Date().getTime();
//y value == user input
int yValue = Integer.parseInt(String.valueOf(inputTextY.getText()));
dbh.insertToData(xValue, yValue);
//grab the data and add to graph
dataseries.resetData(grabData()); //THIS LINE IS CAUSING THE ERROR ACCORDING TO THE LOGCAT
graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(){
@Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX){
return sdf.format(new Date((long)value));
} else {
return super.formatLabel(value, isValueX);
}
}
});
}
});
}
private DataPoint[] grabData() {
String [] column = {"xValue", "yValue"};
//circle through database to grab data and close query after using it
@SuppressLint("Recycle") Cursor cursor = sqLiteDatabase.query("Table1", column, null, null, null, null, null);
DataPoint[] dataPoints = new DataPoint[cursor.getCount()];
//for loop to loop through data to get each data point
for (int i=0; i < cursor.getCount(); i++) {
cursor.moveToNext();
dataPoints[i] = new DataPoint(cursor.getLong(0), cursor.getInt(1));
}
return dataPoints;
}
}
这是数据库的代码 (Java class DatabaseHandler
):
package com.example.fitmania;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DatabaseHandler extends SQLiteOpenHelper {
public DatabaseHandler(Context context) {
super(context, "Database1", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//create table
String CreateTable = "create table Table1 (xValue INTEGER,yValue INTEGER)";
db.execSQL(CreateTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insertToData(long ValX, int ValY) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("xValue",ValX);
contentValues.put("yValue",ValY);
database.insert("Table1", null, contentValues);
}
}
这是我遇到的错误:
2020-03-22 17:40:10.828 27782-27782/com.example.fitmania E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fitmania, PID: 27782
java.lang.IllegalArgumentException: The order of the values is not correct. X-Values have to be ordered ASC. First the lowest x value and at least the highest x value.
at com.jjoe64.graphview.series.BaseSeries.checkValueOrder(BaseSeries.java:532)
at com.jjoe64.graphview.series.BaseSeries.resetData(BaseSeries.java:412)
at com.example.fitmania.wTrackingTestingActivity.onClick(wTrackingTestingActivity.java:66)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12693)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
这是用于图形和用户输入的 XML 文件(activity wTrackingTestingActivity
、XML activity_w_tracking_testing
):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".wTrackingTestingActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/inputTextY"
app:layout_constraintBottom_toTopOf="@+id/graph"
android:hint="Enter your weight"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.522"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/insertButton"
android:layout_alignParentEnd="true"
android:text="Button"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:layout_constraintBottom_toTopOf="@+id/graph"
app:layout_constraintEnd_toEndOf="parent"
android:layout_toEndOf="@+id/inputTextY"
app:layout_constraintTop_toTopOf="parent"
android:layout_toRightOf="@+id/inputTextY"
android:layout_alignParentRight="true" />
<com.jjoe64.graphview.GraphView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/graph"
android:layout_marginStart="16dp"
android:layout_below="@+id/inputTextY"
app:layout_constraintTop_toBottomOf="@+id/inputTextY"
app:layout_constraintVertical_bias="1.0"
android:layout_marginLeft="16dp" />
</RelativeLayout>
错误消息说
java.lang.IllegalArgumentException: The order of the values is not correct. X-Values have to be ordered ASC. First the lowest x value and at least the highest x value.
这意味着在将值传递给 LineGraphSeries<DataPoint>.resetData()
之前,您必须确保值的顺序正确。
在 grabData()
中,您通过
检索值
Cursor cursor = sqLiteDatabase.query("Table1", column, null, null, null, null, null);
查找此方法的 documentation 表明,如果将 null
作为最后一个参数传递,则结果集很可能是无序的(相对于 xValue
列)。
要获得有序的结果集,您必须传入部分 ORDER BY 子句。
整个语句将是
SELECT xValue, yValue FROM Table1 ORDER BY xValue
您只需要传递 "xValue" 作为最后一个参数:
Cursor cursor = sqLiteDatabase.query("Table1", column, null, null, null, null, "xValue");
我是一名 A-level 计算机科学二年级的学生。我有一个课程作业项目,我决定使用 Android Studio 制作一个健身应用程序,但我遇到了一个我无法解决的错误,也不明白为什么会这样。该错误是由(activity)中的代码引起的。我根据 Logcat(这是代码 dataseries.resetData(grabData());
)对该行进行了评论。它位于 public void insertData() {
.
我得到的错误是 java.lang.IllegalArgumentException: The order of the values is not correct. X-Values have to be ordered ASC. First the lowest x value and at least the highest x value.
错误的完整详细信息如下所示。
我使用了 YouTube 上的一个教程来帮助我制作图表视图(因为我想制作一个在 y 轴和日期 x 轴上具有用户输入的图表)。我也完成了视频中所说的一切。 This is the video我用过
我不知道如何解决这个问题,所以到目前为止我唯一做的就是重做代码以确保我没有做错任何事情。但是,我仍然收到错误。我已经尝试联系制作视频的人,但他没有回应,而且可能不会,因为他最近没有在他的社交媒体上上传。
这是图形和用户输入的代码 (activity wTrackingTestingActivity
):
package com.example.fitmania;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.jjoe64.graphview.DefaultLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import java.text.SimpleDateFormat;
import java.util.Date;
public class wTrackingTestingActivity extends AppCompatActivity {
Button insertButton;
EditText inputTextY;
GraphView graphView;
DatabaseHandler dbh;
SQLiteDatabase sqLiteDatabase;
LineGraphSeries<DataPoint> dataseries = new LineGraphSeries<>(new DataPoint[0]);
@SuppressLint("SimpleDateFormat")
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_w_tracking_testing);
insertButton = (Button) findViewById(R.id.insertButton);
inputTextY = (EditText) findViewById(R.id.inputTextY);
graphView = (GraphView) findViewById(R.id.graph);
dbh = new DatabaseHandler(this);
sqLiteDatabase = dbh.getWritableDatabase();
graphView.addSeries(dataseries);
insertData();
}
public void insertData() {
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//set data and add to the database
//x value == date
long xValue = new Date().getTime();
//y value == user input
int yValue = Integer.parseInt(String.valueOf(inputTextY.getText()));
dbh.insertToData(xValue, yValue);
//grab the data and add to graph
dataseries.resetData(grabData()); //THIS LINE IS CAUSING THE ERROR ACCORDING TO THE LOGCAT
graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(){
@Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX){
return sdf.format(new Date((long)value));
} else {
return super.formatLabel(value, isValueX);
}
}
});
}
});
}
private DataPoint[] grabData() {
String [] column = {"xValue", "yValue"};
//circle through database to grab data and close query after using it
@SuppressLint("Recycle") Cursor cursor = sqLiteDatabase.query("Table1", column, null, null, null, null, null);
DataPoint[] dataPoints = new DataPoint[cursor.getCount()];
//for loop to loop through data to get each data point
for (int i=0; i < cursor.getCount(); i++) {
cursor.moveToNext();
dataPoints[i] = new DataPoint(cursor.getLong(0), cursor.getInt(1));
}
return dataPoints;
}
}
这是数据库的代码 (Java class DatabaseHandler
):
package com.example.fitmania;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DatabaseHandler extends SQLiteOpenHelper {
public DatabaseHandler(Context context) {
super(context, "Database1", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//create table
String CreateTable = "create table Table1 (xValue INTEGER,yValue INTEGER)";
db.execSQL(CreateTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insertToData(long ValX, int ValY) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("xValue",ValX);
contentValues.put("yValue",ValY);
database.insert("Table1", null, contentValues);
}
}
这是我遇到的错误:
2020-03-22 17:40:10.828 27782-27782/com.example.fitmania E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fitmania, PID: 27782
java.lang.IllegalArgumentException: The order of the values is not correct. X-Values have to be ordered ASC. First the lowest x value and at least the highest x value.
at com.jjoe64.graphview.series.BaseSeries.checkValueOrder(BaseSeries.java:532)
at com.jjoe64.graphview.series.BaseSeries.resetData(BaseSeries.java:412)
at com.example.fitmania.wTrackingTestingActivity.onClick(wTrackingTestingActivity.java:66)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12693)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
这是用于图形和用户输入的 XML 文件(activity wTrackingTestingActivity
、XML activity_w_tracking_testing
):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".wTrackingTestingActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/inputTextY"
app:layout_constraintBottom_toTopOf="@+id/graph"
android:hint="Enter your weight"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.522"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/insertButton"
android:layout_alignParentEnd="true"
android:text="Button"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:layout_constraintBottom_toTopOf="@+id/graph"
app:layout_constraintEnd_toEndOf="parent"
android:layout_toEndOf="@+id/inputTextY"
app:layout_constraintTop_toTopOf="parent"
android:layout_toRightOf="@+id/inputTextY"
android:layout_alignParentRight="true" />
<com.jjoe64.graphview.GraphView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/graph"
android:layout_marginStart="16dp"
android:layout_below="@+id/inputTextY"
app:layout_constraintTop_toBottomOf="@+id/inputTextY"
app:layout_constraintVertical_bias="1.0"
android:layout_marginLeft="16dp" />
</RelativeLayout>
错误消息说
java.lang.IllegalArgumentException: The order of the values is not correct. X-Values have to be ordered ASC. First the lowest x value and at least the highest x value.
这意味着在将值传递给 LineGraphSeries<DataPoint>.resetData()
之前,您必须确保值的顺序正确。
在 grabData()
中,您通过
Cursor cursor = sqLiteDatabase.query("Table1", column, null, null, null, null, null);
查找此方法的 documentation 表明,如果将 null
作为最后一个参数传递,则结果集很可能是无序的(相对于 xValue
列)。
要获得有序的结果集,您必须传入部分 ORDER BY 子句。
整个语句将是
SELECT xValue, yValue FROM Table1 ORDER BY xValue
您只需要传递 "xValue" 作为最后一个参数:
Cursor cursor = sqLiteDatabase.query("Table1", column, null, null, null, null, "xValue");