简体   繁体   中英

Passing data to custom view

I want to pass data to my custom view, a list. It worked but for some reasons I get now a Nullpointer exception and I have no idea why.

I pass the data through the constructor but the list size is always zero but when I try to get the size of the list IN the constructor I get the right size but outside of it always 0.

MainActivity.java

LineChart2 lineChart2;
List<items> items = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    items.add(new items(R.color.babyBlau,10,"Internet"));
    items.add(new items(R.color.colorAccent,20,"Internet"));
    items.add(new items(R.color.rot,60,"Internet"));

    lineChart2 = new LineChart2(MainActivity.this,items);


}

LineChart2.java package com.example.viewtest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

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

public class LineChart2 extends View {

List<items> items = new ArrayList<>();
Paint paintThinBlackLine;
Paint paintThinGrayLines;
Paint paintText;
float padding = 50;
float padding_left = padding * 3;
float padding_text = padding_left/2;

public LineChart2(Context context, List<com.example.viewtest.items> items) {
    super(context);
    this.items = items;
    initPaints();
}

public LineChart2(Context context, AttributeSet attrs) {
    super(context, attrs);
    initPaints();
}

public LineChart2(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initPaints();
}

public LineChart2(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initPaints();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(width,width/2);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // thin lines
    for(int i = 1; i < 7; i++){
        float paddingbetween = (getHeight()-padding-padding)/6;
        canvas.drawLine(padding_left,paddingbetween*i,getWidth()-padding,paddingbetween*i,paintThinGrayLines);
        Log.d("HALLO",""+items.size());
            //canvas.drawText(String.valueOf(items.get(i).value),padding_text,paddingbetween*i,paintText);

    }
}

public void initPaints(){
    paintThinBlackLine = new Paint();
    paintThinBlackLine.setColor(Color.parseColor("#6FB4B3B3"));
    paintThinBlackLine.setStyle(Paint.Style.STROKE);
    paintThinBlackLine.setStrokeCap(Paint.Cap.ROUND);
    paintThinBlackLine.setStrokeWidth(3);

    paintThinGrayLines = new Paint();
    paintThinGrayLines.setColor(Color.parseColor("#6FB4B3B3"));
    paintThinGrayLines.setStyle(Paint.Style.STROKE);
    paintThinGrayLines.setStrokeCap(Paint.Cap.ROUND);
    paintThinGrayLines.setStrokeWidth(2);

    paintText = new Paint();
    paintText.setTextSize(20);
    paintText.setColor(Color.parseColor("#FF696969"));
    paintText.setTextAlign(Paint.Align.CENTER);

}

}

I have compiled your code and it works well. the only reason that will generate the issue is attaching the layout to it's container before settings the data.

It's better to add your layout from the code like below.

    LinearLayout anyDesiredLayout= findViewById(R.id.layout);

    items.add(new items(R.color.colorAccent, 10, "Internet"));
    items.add(new items(R.color.colorAccent, 20, "Internet"));
    items.add(new items(R.color.colorAccent, 60, "Internet"));

    LineChart2 lineChart2 = new LineChart2(this, items);

    anyDesiredLayout.addView(lineChart2);

EDIT

The issue is that you're creating two views

This line new LineChart2(this, items); will create new view which is different than the view that you have attached in your xml.

In lineChart2 case you can see that items has been set correctly in lineChart2 constructor. But onDraw hasn't been invoked because you didn't attach the view to any container.

And the view in your xml onDraw method will be invoked but you will find items list is null. Because LineChart2(Context context, List<items> items) has never been invoked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM