gpt4 book ai didi

java - 在Android中加载XML资源

转载 作者:行者123 更新时间:2023-12-02 12:32:39 26 4
gpt4 key购买 nike

我正在尝试让这个图表包工作:

https://github.com/PhilJay/MPAndroidChart/wiki/Getting-Started

我遇到的问题不在于包,而是我不了解如何/在何处定义 XML 以及如何从代码中引用它。

这是我绘制 LineGraph 的类(class):

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import android.support.v7.app.AppCompatActivity;


import java.util.ArrayList;
import java.util.List;

public class ChartHandler extends AppCompatActivity {

public void initLineChart() {
// in this example, a LineChart is initialized from xml
LineChart chart = (LineChart) findViewById(R.id.chart);

ArrayList<Integer> data = new ArrayList<Integer>();

data.add(10);
data.add(20);
data.add(30);
data.add(40);
data.add(50);

List<Entry> entries = new ArrayList<Entry>();

for (Integer item : data) {
// turn your data into Entry objects
entries.add(new Entry(item, item));
}

LineDataSet dataSet = new LineDataSet(entries, "Label"); // add entries to dataset
dataSet.setColor(170);
dataSet.setValueTextColor(0);

LineData lineData = new LineData(dataSet);
chart.setData(lineData);
chart.invalidate(); // refresh

}
}

我不清楚在哪里定义图表的 XML,我已将其添加为 value/linechart.xml 下的新文件:

<?xml version="1.0" encoding="utf-8"?>

当我构建时,我收到一个错误,Charthandler.java 找不到符号图表。

注意:如果我将此 XML 添加到现有的 content_main.xml 布局中,然后将代码复制到 MainActivity 的 onCreate 函数中,它就可以工作,但我希望能够在单独的类和 XML 定义中组织代码在他们自己的文件中

更新:

我创建了一个新文件 res/layout/linechart.xml

<?xml version="1.0" encoding="utf-8"?>
<com.github.mikephil.charting.charts.LineChart xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/chart"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
</com.github.mikephil.charting.charts.LineChart>

构建完成,但当我的代码尝试查找 id 时出现运行时错误:

07-20 11:38:10.361 1290-1290/com.testapp.cloudsnifferclient E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.testapp.cloudsnifferclient, PID: 1290
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testapp.cloudsnifferclient/com.testapp.cloudsnifferclient.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.LineChart.setData(com.github.mikephil.charting.data.ChartData)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.LineChart.setData(com.github.mikephil.charting.data.ChartData)' on a null object reference
at com.testapp.MainActivity.onCreate(MainActivity.java:71)
at android.app.Activity.performCreate(Activity.java:6288)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5942) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

最佳答案

将其放在 res/layout 下即可解决您的问题

关于java - 在Android中加载XML资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45218492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com