我正在尝试将GraphView添加到我自定义的对话框弹出窗口中,但每次都会收到NullPointerException。
我已经单独测试了GraphView和Dialog popup,所以我知道它们都是正确的。我只是无法在对话框中实现GraphView。 如标题中所述,所有这些都位于片段中,
这里是对话框和graphview的代码:
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);
我希望有人知道可能出现的问题。如果需要更多信息,请询问。
非常感谢任何帮助。
答案 0 :(得分:1)
问题问题已经过去了大约1年,但我会留下答案以防万一有人需要。
根据您的xml文件,您需要的GraphView
元素是popup_statistic_exercise
布局的元素。因此,在使用GraphView
布局的对话框的View
后,您需要获取popup_statistic_exercise
对象。
// 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();