Android variable from activity to view

210 Views Asked by At

I try to draw with a variable from a activity. When I call the getOff method, the program crashed. Do I have to declare the object? Or what do I wrong?

This is my ActivityClass:

public class MyActivity extends Activity { 

PaintViewWZPGrenzwinkel paintviewgrenzwinkel;

public void setPixel(double pix) { 
    double pixel = pix; 
    double oeffnunginradian = 2 * Math.atan((B / 2) * (1 / f)); 
    double oeffnunginwinkel = Math.toDegrees(oeffnunginradian); 
    double oeffnungswinkel = pixel * (a1 / oeffunginwinkel); 
    paintviewgrenzwinkel.getOff(oeffnungswinkel); 
} 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            paintviewgrenzwinkel = (PaintViewWZPGrenzwinkel) findViewById(R.id.paintViewWZPGrenzwinkel1);

....}

Here I get the variable and draw it.

public class PaintViewWZPGrenzwinkel extends View{

double oeffnungswinkel;
public void getOff(double ow) {
    oeffnungswinkel = ow;
    invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeWidth(10);
    paint.setColor(Color.WHITE);
    float width = (float) getWidth() / 2;
    float height = (float) getHeight() / 2;
    float oeffnungswinkel1 = (float) (width - ((oeffnungswinkel / 2) + 5));
    float oeffnungswinkel2 = (float) (width + ((oeffnungswinkel / 2) + 5));
    canvas.drawLine(oeffnungswinkel1, height - 100, oeffnungswinkel1, height + 100, paint);
    canvas.drawLine(oeffnungswinkel2, height - 100, oeffnungswinkel2, height + 100, paint);
}

Here is my LogCat:

12-11 10:14:18.382: W/System.err(12939): java.lang.NullPointerException
12-11 10:14:18.382: W/System.err(12939):    at de.fovea.waldinvent.WinkelzaehlprobeActivity.setPixel(WinkelzaehlprobeActivity.java:61)
12-11 10:14:18.382: W/System.err(12939):    at de.fovea.views.CameraViewWZP.updateZoomStatus(CameraViewWZP.java:62)
12-11 10:14:18.382: W/System.err(12939):    at de.fovea.waldinvent.WinkelzaehlprobeActivity$2.onProgressChanged(WinkelzaehlprobeActivity.java:110)
12-11 10:14:18.382: W/System.err(12939):    at android.widget.SeekBar.onProgressRefresh(SeekBar.java:96)
12-11 10:14:18.382: W/System.err(12939):    at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:684)
12-11 10:14:18.382: W/System.err(12939):    at android.widget.ProgressBar.refreshProgress(ProgressBar.java:709)
12-11 10:14:18.382: W/System.err(12939):    at android.widget.ProgressBar.setProgress(ProgressBar.java:799)
12-11 10:14:18.382: W/System.err(12939):    at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:655)
12-11 10:14:18.382: W/System.err(12939):    at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:574)
12-11 10:14:18.382: W/System.err(12939):    at android.view.View.dispatchTouchEvent(View.java:7817)
12-11 10:14:18.382: W/System.err(12939):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
12-11 10:14:18.382: W/System.err(12939):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
12-11 10:14:18.392: W/System.err(12939):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2326)
12-11 10:14:18.392: W/System.err(12939):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1612)
12-11 10:14:18.392: W/System.err(12939):    at android.app.Activity.dispatchTouchEvent(Activity.java:2494)
12-11 10:14:18.392: W/System.err(12939):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2274)
12-11 10:14:18.392: W/System.err(12939):    at android.view.View.dispatchPointerEvent(View.java:8025)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4659)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4547)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4148)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4198)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4167)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4252)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4175)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4309)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4148)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4198)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4167)
12-11 10:14:18.392: W/System.err(12939):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4175)
12-11 10:14:18.402: W/System.err(12939):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4148)
12-11 10:14:18.402: W/System.err(12939):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6293)
12-11 10:14:18.402: W/System.err(12939):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6231)
12-11 10:14:18.402: W/System.err(12939):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6202)
12-11 10:14:18.402: W/System.err(12939):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6373)
12-11 10:14:18.402: W/System.err(12939):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:188)
12-11 10:14:18.402: W/System.err(12939):    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
12-11 10:14:18.402: W/System.err(12939):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:177)
12-11 10:14:18.402: W/System.err(12939):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6346)
12-11 10:14:18.402: W/System.err(12939):    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6392)
12-11 10:14:18.402: W/System.err(12939):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
12-11 10:14:18.402: W/System.err(12939):    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
12-11 10:14:18.402: W/System.err(12939):    at android.view.Choreographer.doFrame(Choreographer.java:559)
12-11 10:14:18.402: W/System.err(12939):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
12-11 10:14:18.402: W/System.err(12939):    at android.os.Handler.handleCallback(Handler.java:730)
12-11 10:14:18.402: W/System.err(12939):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 10:14:18.402: W/System.err(12939):    at android.os.Looper.loop(Looper.java:137)
12-11 10:14:18.402: W/System.err(12939):    at android.app.ActivityThread.main(ActivityThread.java:5419)
12-11 10:14:18.402: W/System.err(12939):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 10:14:18.402: W/System.err(12939):    at java.lang.reflect.Method.invoke(Method.java:525)
12-11 10:14:18.402: W/System.err(12939):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
12-11 10:14:18.402: W/System.err(12939):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
12-11 10:14:18.402: W/System.err(12939):    at dalvik.system.NativeStart.main(Native Method)
1

There are 1 best solutions below

1
On

Change-

paintviewgrenzwinkel = (PaintViewWZPGrenzwinkel) findViewById(R.id.paintViewWZPGrenzwinkel1);

Into-

paintviewgrenzwinkel = (PaintViewGrenzwinkel) findViewById(R.id.paintViewWZPGrenzwinkel1);

Hope its work proper now.