As this answer,
I need to pass a value from my main Activity to a custom View.After clicked a button, the createBall()
will be called.And then pass the values to custom view.But after I click the button I get this error.
com.example.ball E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ball, PID: 12141
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ball.customView.fresh(int, int, int)' on a null object reference
here is my MainActivity
public class MainActivity extends AppCompatActivity {
private WindowManager wml;
private customView myCus;
...
private void createBall(int radius) {
if (radius > 150) return;
WindowManager wm1 = this.getWindowManager();
int width = wm1.getDefaultDisplay().getWidth();
int height = wm1.getDefaultDisplay().getHeight();
myCus.fresh(width, height, radius);
}
}
here is my custom view
...
public void fresh(int width, int height, int r) {
radius = (int)r;
x = (int)Math.random() * (width - radius * 2);
y = (int)Math.random() * (height - radius * 2);
...
invalidate();
}
Can you fix it for me, thx.