gpt4 book ai didi

android - 在 fragment 中的自定义构建对话框中使用 Android GraphView

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

我正在尝试将 GraphView 添加到我的自定义对话框弹出窗口中,但我每次都收到 NullPointerException。

我已经分别测试了 GraphView 和 Dialog 弹出窗口,所以我知道它们都是正确的。我只是无法在对话框中实现 GraphView。如标题所述,所有这些都位于 fragment 中,

这里是对话框和图形 View 的代码:

public void showStatisticWindow(Exercise exercise, ViewGroup cont) {
// Get the graph
GraphView graph = (GraphView) v.findViewById(R.id.graph_exercise_statistic);

BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, -2),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);

// Create the dialog / popup
final View popupExerciseStatisticView = View.inflate(cont.getContext(), R.layout.popup_statistic_exercise, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(cont.getContext());
final AlertDialog alert = builder.create();

alert.setTitle("Create the program");
alert.setView(popupExerciseStatisticView);
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
} });
alert.show();

}

这是对话框的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/popup_statistic_exercise">

<com.jjoe64.graphview.GraphView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/graph_exercise_statistic" />

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/graph_relativelayout">

<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Sets"
android:id="@+id/sets_names_headline"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@color/blue_bg_inputField"
android:textAlignment="viewStart"
android:padding="10dp" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/sets_names_headline"
android:layout_alignLeft="@+id/sets_names_headline"
android:layout_alignStart="@+id/sets_names_headline"
android:background="@color/blue_bg_inputField"
android:paddingLeft="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:id="@+id/sets_names_list"></LinearLayout>

</RelativeLayout>

</RelativeLayout>

最后,这是控制台的输出:

java.lang.NullPointerException
at com.mobileplatformexam.fragments.StatisticExercisesFragment.showStatisticWindow(StatisticExercisesFragment.java:153)
at com.mobileplatformexam.fragments.StatisticExercisesFragment$1.onTouch(StatisticExercisesFragment.java:87)
at android.view.View.dispatchTouchEvent(View.java:5553)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2038)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1396)
at android.app.Activity.dispatchTouchEvent(Activity.java:2535)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1986)
at android.view.View.dispatchPointerEvent(View.java:5738)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2900)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2476)
at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:849)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:863)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)

第153行的代码是:

 graph.addSeries(series);

我希望有人知道哪里出了问题。如果需要更多信息,请询问。

任何帮助将不胜感激。

最佳答案

自问这个问题以来已经过去了大约 1 年,但我会留下我的答案以备不时之需。

根据您的 xml 文件,您想要的 GraphView 元素是 popup_statistic_exercise 布局的一个元素。因此,在使用 popup_statistic_exercise 布局的对话框的 View 膨胀后,您需要获取 GraphView 对象。

// Create the dialog / popup
final View popupExerciseStatisticView = View.inflate(cont.getContext(), R.layout.popup_statistic_exercise, null);

// Get the graph
GraphView graph = (GraphView) popupExerciseStatisticView.findViewById(R.id.graph_exercise_statistic);

BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, -2),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);

final AlertDialog.Builder builder = new AlertDialog.Builder(cont.getContext());
final AlertDialog alert = builder.create();

alert.setTitle("Create the program");
alert.setView(popupExerciseStatisticView);
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
} });
alert.show();

关于android - 在 fragment 中的自定义构建对话框中使用 Android GraphView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29931957/

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