Skip to content
Advertisement

How to Call function in custom view in android studio activity

MainActivity.java

package com.bandiapps.graphicfuture;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private CustomView cv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cv = findViewById(R.id.cus_1);

        cv.setWidth();
    }
}

CustomView.java

package com.bandiapps.graphicfuture;

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


public class CustomView extends View {
    int width,height;
    private Paint paint;
    public CustomView(Context context, AttributeSet attrs) {
        super(context);
        paint = new Paint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.STROKE);
        paint.setARGB(255,0,255,0);
        canvas.drawRect(1,1,width -1 ,height -1 ,paint);
    }
    public void setWidth(){
        paint.setStrokeWidth(10);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">
    <com.bandiapps.graphicfuture.CustomView
        android:id="@+id/cus_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="100dp"
        />

</RelativeLayout>

when I going to run that code in my device it keep closing. Log cat shows error like this.

 E  FATAL EXCEPTION: main
                                                                                                    Process: com.bandiapps.graphicfuture, PID: 25635
                                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bandiapps.graphicfuture/com.bandiapps.graphicfuture.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.bandiapps.graphicfuture.CustomView.setMessage()' on a null object reference
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3846)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4022)
                                                                                                        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
                                                                                                        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
                                                                                                        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loop(Looper.java:246)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8653)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
                                                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.bandiapps.graphicfuture.CustomView.setMessage()' on a null object reference
                                                                                                        at com.bandiapps.graphicfuture.MainActivity.onCreate(MainActivity.java:19)
                                                                                                        at android.app.Activity.performCreate(Activity.java:8207)
                                                                                                        at android.app.Activity.performCreate(Activity.java:8191)
                                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3819)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4022) 
                                                                                                        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
                                                                                                        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
                                                                                                        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336) 
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                                        at android.os.Looper.loop(Looper.java:246) 
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8653) 
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 

I use physical device to implement this code. I’m beginner android developer also. I follow different kind of tutorials but I could not be able to solve it.I want to use this custom component same as normal widgets like buttons,Edit Text.I also want to place this code using xml also. I cannot able to change it. because my target project is already developed using xml layouts. How can I solve it ?

Advertisement

Answer

The problem is that you are not passing the attrs to the super. This causes it to not have an id

Change your constructor to

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement