要求用户输入值并在 android studio kotlin 中使用 graphview 制作图表

问题描述 投票:0回答:0

我正在尝试制作一个应用程序,用户可以在其中输入自己的点坐标(x,y 代表 4 个不同的点),然后使用 Android GraphView Lib 将它们显示在图表上。但是,一旦我输入了值并按下计算按钮,第二页就会崩溃并且不会显示这些点。 这似乎是一个 Id 问题,但我不知道我必须改变什么才能让它工作。

这是我拥有的不同页面,XML 和 kt: activity_accueil.xml 这是要求用户输入值的第一个页面 看起来像这样: look of the page part of the code

这里是 Accueil.kt 的代码: 包 com.example.position_plaques

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class Acceuil : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_acceuil)

        title="Coordonnées connues "

        val button:Button = findViewById(R.id.button)
        button.setOnClickListener{
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
    }
}

接下来是我要显示坐标的第二页: activity_main.xml:

<TextView
        android:id="@+id/idTVHead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:gravity="center"
        android:padding="4dp"
        android:text="@string/app_name"
        android:textAlignment="center"
        android:textColor="@color/purple_200"
        android:textSize="20sp"
        android:textStyle="bold" />

    <!--line graph view where we will
        be displaying our data-->
    <com.jjoe64.graphview.GraphView
        android:id="@+id/idGraphView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/idTVHead" />
        <!--tools:ignore="MissingClass"-->

</RelativeLayout>

对于 MainActivity.kt:

package com.example.position_plaques

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.jjoe64.graphview.GraphView
import com.jjoe64.graphview.GridLabelRenderer
import com.jjoe64.graphview.series.DataPoint
import com.jjoe64.graphview.series.LineGraphSeries
import com.jjoe64.graphview.series.PointsGraphSeries
import kotlin.math.abs
import kotlin.math.sin

class MainActivity : AppCompatActivity() {

        // on below line we are creating
        // variables for our graph view


    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // on below line we are initializing
        // our variable with their ids.

        val graphView: GraphView = findViewById(R.id.idGraphView)



        val sxAl: EditText = findViewById(R.id.xAl)
        val syAl: EditText = findViewById(R.id.yAl)
        val sxBl: EditText = findViewById(R.id.xBl)
        val syBl: EditText = findViewById(R.id.yBl)

        val xAl = sxAl.text.toString().toDouble()
        val yAl = syAl.text.toString().toDouble()
        val xBl = sxBl.text.toString().toDouble()
        val yBl = syBl.text.toString().toDouble()


        val series: LineGraphSeries<DataPoint> = LineGraphSeries(
            arrayOf(
                DataPoint(xAl, yAl),
                DataPoint(xBl, yBl)
            )
        )

        // on below line adding animation
        graphView.animate()

        // below line is to add series
        // to our graph view.
        graphView.addSeries(series)

        // below line is to activate
        // horizontal scrolling.
        graphView.viewport.isScrollable = true

        // below line is to activate horizontal
        // zooming and scrolling.
        graphView.viewport.isScalable = true

        // below line is to activate vertical and
        // horizontal zoom with scrolling.
        graphView.viewport.setScalableY(true)

        // below line is to activate vertical scrolling.
        graphView.viewport.setScrollableY(true)


        val gridLabelx: GridLabelRenderer = graphView.gridLabelRenderer
        gridLabelx.horizontalAxisTitle = "shooting line"

        val gridLabely: GridLabelRenderer = graphView.gridLabelRenderer
        gridLabely.verticalAxisTitle= "10-meter line"


        graphView.viewport.isYAxisBoundsManual = true;
        graphView.viewport.isXAxisBoundsManual = true;

    }


}

当我尝试运行时,它说:findViewById(R.id.idGraphView) must not be null

我可以改变什么让它工作?

android kotlin android-graphview
© www.soinside.com 2019 - 2024. All rights reserved.