gpt4 book ai didi

android - textView 上的数据绑定(bind)空指针异常

转载 作者:行者123 更新时间:2023-12-03 20:50:56 27 4
gpt4 key购买 nike

我知道这个异常(exception)是什么,并且我知道有很多类似的问题。但我无法将这些解决方案应用于我的问题。当我使用 findById() 时一切正常但是当我尝试使用数据绑定(bind)时,我被卡住了。我是数据绑定(bind)的新手。

public class HomePieChartMarkerViewModel extends MarkerView {
private TvContentBinding binding;

/**
* Constructor. Sets up the MarkerView with a custom layout resource.
*
* @param context
* @param layoutResource the layout resource to use for the MarkerView
*/
public HomePieChartMarkerViewModel(Context context, int layoutResource) {
super(context, layoutResource);
}


@Override
public void refreshContent(Entry e, Highlight highlight) {
binding.markerValue.setText("$ " + e.getY());// <<<< HERE I GOT AN ERROR
super.refreshContent(e,highlight);
}
private MPPointF mOffset;

@Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
}

return mOffset;
}
}
我在我的 fragment 中调用这个类:
IMarker marker = new HomePieChartMarkerViewModel(context,R.layout.tv_content);
binding.myMonthlyExpenditureSingleLineChart.setMarker(marker);
我的 xml 电视内容
<layout>

<data>

<variable
name="chart_marker"
type="com.nswd.successplan.model.HomePieChartMarkerViewModel" />
</data>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:background="@drawable/tv_content_rounded"
android:gravity="center">

<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_bulet_blue"
android:paddingEnd="8dp"/>
<TextView
android:id="@+id/marker_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="985"
android:textSize="12sp"
android:textColor="@color/background"/>
</LinearLayout>

编辑:我的错误
java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.nswd.successplan.databinding.TvContentBinding.markerValue' on a null object reference
at com.nswd.successplan.model.HomePieChartMarkerViewModel.refreshContent(HomePieChartMarkerViewModel.java:32)
at com.github.mikephil.charting.charts.Chart.drawMarkers(Chart.java:741)
at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:285)
at android.view.View.draw(View.java:23191)
at android.view.View.updateDisplayListIfDirty(View.java:22066)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
at android.view.View.updateDisplayListIfDirty(View.java:22021)
at android.view.View.draw(View.java:22921)
at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
at android.view.View.draw(View.java:23194)
at androidx.core.widget.NestedScrollView.draw(NestedScrollView.java:1931)
at android.view.View.updateDisplayListIfDirty(View.java:22066)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
at android.view.View.updateDisplayListIfDirty(View.java:22021)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
at android.view.View.updateDisplayListIfDirty(View.java:22021)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
at android.view.View.updateDisplayListIfDirty(View.java:22021)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
at android.view.View.updateDisplayListIfDirty(View.java:22021)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
at android.view.View.updateDisplayListIfDirty(View.java:22021)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
at android.view.View.updateDisplayListIfDirty(View.java:22021)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
at android.view.View.updateDisplayListIfDirty(View.java:22021)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
at android.view.View.updateDisplayListIfDirty(View.java:22021)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
at android.view.View.updateDisplayListIfDirty(View.java:22021)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:588)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:594)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:667)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:4263)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:4047)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3320)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2200)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8999)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:996)
at android.view.Choreographer.doCallbacks(Choreographer.java:794)
at android.view.Choreographer.doFrame(Choreographer.java:729)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:981)
at android.os.Handler.handleCallback(Handler.java:883)

最佳答案

尝试使用这个

  ((HomePieChartMarkerViewModel) context).binding.myMonthlyExpenditureSingleLineChart.setMarker(marker);
  • 我在尝试访问 的内容时遇到了同样的错误 TextView 来自我的回收器 View 适配器类中的 MainActivity。因此,通过使用类的上下文,空指针消失了。这对我有帮助。我在 viewBinder 函数中调用了它,其中有 。上下文 也是上一课的。

  • 我希望我回答了你的问题。

    关于android - textView 上的数据绑定(bind)空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62846433/

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