将自定义视图添加到片段

时间:2019-02-20 15:49:51

标签: android fragment custom-view

我想在片段中添加一个自定义视图类,但是有些不对。好像我忘记了什么,因为我得到了这个异常:

“尝试在com.example.stresssensorapp.Quadrant.DrawLabelPointView.onDraw上的空对象引用上调用虚拟方法'void android.graphics.Paint.setARGB(int,int,int,int)'”

这是我的代码

片段类:

package com.example.stresssensorapp.Fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.stresssensorapp.Quadrant.DrawLabelPointView;
import com.example.stresssensorapp.R;

public class Item2Fragment extends Fragment {
    public static Item2Fragment newInstance() {
        Item2Fragment fragment = new Item2Fragment();
        return fragment;
    }

    private DrawLabelPointView mQuadrantView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item2, container, false);
        mQuadrantView = (DrawLabelPointView) view.findViewById(R.id.quadrantView);


        return view;


    }
}

自定义类别:

package com.example.stresssensorapp.Quadrant;

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

public class DrawLabelPointView extends View implements View.OnTouchListener {

    private static final String TAG = "DrawLabelPointView";

    static int x,y,r=255,g=255,b=255;
    final static int radius=30;
    Paint paint;     //using this ,we can draw on canvas

    public DrawLabelPointView(Context context)
    {
        super(context);
        paint=new Paint();
        paint.setAntiAlias(true);       //for smooth rendering
        paint.setARGB(255, r, g, b);    //setting the paint color

        //to make it focusable so that it will receive touch events properly
        setFocusable(true);

        //adding touch listener to this view
        this.setOnTouchListener(this);
    }
    public DrawLabelPointView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawLabelPointView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }



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

        paint.setARGB(255, r, g, b);

        //drawing the circle
        canvas.drawCircle(x,y,radius,paint);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x=(int)event.getX()-(radius/2);      //some math logic to plot the circle  in exact touch place
        y=(int)event.getY()-(radius/2);
        //System.out.println("X,Y:"+"x"+","+y);      //see this output in "LogCat"
        randColor();       //calls this method to generate a color before drawing
        invalidate();      //calls onDraw method
        return true;
    }

    public void randColor()
    {
        r=(int)(Math.random()*255);
        g=(int)(Math.random()*255);
        b=(int)(Math.random()*255);
        //Toast.makeText(c, "r,g,b="+r+","+g+","+b,Toast.LENGTH_SHORT).show();
    }

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/graps_question"
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:layout_centerInParent="true"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:src="@mipmap/question"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.example.stresssensorapp.Quadrant.DrawLabelPointView
        android:id="@+id/quadrantView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:background="@color/primaryColor"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/graps_question" />

</android.support.constraint.ConstraintLayout>

知道我在做什么,还是忘记了? 谢谢!

2 个答案:

答案 0 :(得分:1)

您需要将paint初始化移动到单独的方法:

public DrawLabelPointView(Context context) {
    super(context);
    init();     
}

public DrawLabelPointView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public DrawLabelPointView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    paint = new Paint();
    paint.setAntiAlias(true);       //for smooth rendering
    paint.setARGB(255, r, g, b);    //setting the paint color

    //to make it focusable so that it will receive touch events properly
    setFocusable(true);

    //adding touch listener to this view
    this.setOnTouchListener(this);
}

然后从所有构造函数中调用。

发生这种情况是因为当您在xml中声明视图时,视图将由DrawLabelPointView(Context context, AttributeSet attrs)DrawLabelPointView(Context context, AttributeSet attrs, int defStyle)

构造。

答案 1 :(得分:0)

我认为您也需要为属性构造函数添加绘画对象初始化。 因为从xml开始,第一个构造函数不会调用.. 您需要这样指定

Y predicted (sample 14): 3, 7, 9, 12
Y predicted (sample 65): 5, 8, 9, 11

以及您想要的内容,然后在所有构造函数中调用此方法, 进一步,您可以根据需要检查从xml传递的attrs

相关问题