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我用过。
我不知道如何解决此问题,所以到目前为止,我唯一要做的就是重做代码,以确保我没有做错任何事情。但是,我仍然会收到错误消息。我曾尝试与制作视频的人联系,但他没有回应,可能不会,因为他最近没有在社交媒体上上传。
这是图形和用户输入的代码(活动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类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$1.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文件(活动wTrackingTestingActivity
,XMLactivity_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:值的顺序不正确。 X值必须按ASC顺序订购。首先是最低的x值,至少是最高的x值。
这意味着在将值传递到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");