简体   繁体   中英

Android - Getting a null pointer exception when calling custom View method

I'm new to android and i'm trying to call a method that moves a circle drawn by a custom view using canvas up by 10 pixels. When i try to call said method, i get a null pointer exception and i can't figure out why. Here is what i have.

Main Activity

public class CustomView extends Activity {
XView xv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customview);
}

public void moveUp(View view){
    xv.setY(view);
}

}

The Custom View

public class XView extends View implements View.OnClickListener{

private Paint mPaint = new Paint();
int oX = 200;
int oY = 200;


public XView(Context context, AttributeSet attrs){
    super(context, attrs);
    //setOnClickListener(this);
}



@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(Color.MAGENTA);

    canvas.drawColor(Color.WHITE);
    canvas.drawCircle(oX, oY, 50, mPaint);
}
public void onClick(View view){
    oX=oX+10;
    view.postInvalidate();
}


public void setY(View view)
{
    oY=oY+10;
    view.postInvalidate();
}
}

The XML for the main activity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".customview"
android:orientation="vertical">

<com.mycompany.customview.XView
    android:layout_width="wrap_content"
    android:layout_height="200dp" />


<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignStart="@+id/button2"
        android:onClick="moveUp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_alignTop="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button"
        android:layout_centerHorizontal="true"
        android:id="@+id/button2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/button" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:id="@+id/button5"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignStart="@+id/button2" />
</RelativeLayout>

Thanks

you need to add an id to your custom view in your xml:

  <com.mycompany.customview.XView
    android:id="@+id/xvview_id"
    android:layout_width="wrap_content"
    android:layout_height="200dp" />

and use findViewById to initialize your member

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customview);
    xv = (XView) findViewById(R.id.xvview_id);
}

When you call setContentView , Android "adds", the widgets that you declared in your layout as part of the Activity's view hierarchy, making them available for you in the context of the current activity. What you have to do is just call findViewById to grab a reference to the widget you want to work with

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