XY-Pad得到X和Y NPE错误

时间:2014-01-21 02:53:58

标签: java android object view ontouchevent

我最近一直在为一个Android应用程序开发一个程序,它是一个简单的XY-Pad,它将在set View中显示用户输入的X和Y坐标。它的工作方式是我有一个应用程序的XML布局,它有两个TextViews(一个显示X值,一个显示Y值),它还有一个View,它是XY-Pad所在的位置。我一直遇到的问题是当我尝试将X和Y坐标发送到XML的链接类文件来设置TextViews的文本时,我收到一个错误(NullPointerException错误)。有谁知道代码可能出错?

这是XML数据:

 <RelativeLayout 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"

tools:context=".Start" >

<TextView
    android:id="@+id/theX"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="X:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/theY"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_marginBottom="30dp"
    android:layout_toLeftOf="@+id/linearLayout1"
    android:text="Y: "
    android:textAppearance="?android:attr/textAppearanceMedium" />

<view class="com.example.robotcontroller.drawing"
    android:id="@+id/XYpad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/button1" />

这是'drawing'类,它位于XML数据中的View内部:

 public class drawing extends View{

  private Paint paint = new Paint();
  private Path path = new Path();

  private float eventX = 0;
  private float eventY = 0;

  public drawing(Context context, AttributeSet attrs) {
    super(context, attrs);
    paint.setAntiAlias(true);
    paint.setStrokeWidth(20f);
    paint.setColor(Color.YELLOW);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
  }

  @SuppressLint("DrawAllocation")
@Override
  protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);   
    try{            
         Start itUp = new Start();
         itUp.setXY(eventX, eventY); //The call to the method that is being problematic
        }
        catch(Exception e1){
            Toast.makeText(drawing.this.getContext(), "We have an error!  "+ e1, Toast.LENGTH_LONG).show();
        }
  }


  @Override
  public boolean onTouchEvent(MotionEvent event) {
    try{          
     eventX = event.getX();
     eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:         
      path.moveTo(eventX, eventY);  
      return true;
    case MotionEvent.ACTION_MOVE:
        path.reset();
        path.addCircle(eventX, eventY, 20, Path.Direction.CW);
      break;
    case MotionEvent.ACTION_UP:
        path.reset();
      break;
      default:
          path.reset();
      return false;
    }
    // Schedules a repaint.
    invalidate();
    return true;

  }
    catch(Exception e1){
        return true;
    }
  }


 }

视图可以很好地显示并且可以跟踪您的手指,并且它可以在没有问题的情况下调用另一个类中的另一个方法。问题是,当我想创建一个TextView对象来操作XML中的两个TextView时,程序崩溃了,我得到一个'NullPointerException',我不明白为什么因为该项确实存在并且它包含文本,所以为什么它会不存在吗?

这是'开始'活动,其中包含有问题的'setXY'方法:

 public class Start extends Activity {


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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.start, menu);
    return true;
}


public void setXY(float x, float y){ //The method where the TextViews are created, and pointing to null objects....
    TextView tv1 = (TextView) findViewById(R.id.theX);
    TextView tv2 = (TextView) findViewById(R.id.theY);
    tv1.setText("X value: "+x);     
    tv2.setText("Y value: "+y);
}




 }

这是NPE堆栈跟踪(希望它有帮助......):

 at android.widget.Toast.show(Toast.java:113)
at com.example.robotcontroller.drawing.onDraw(drawing.java:44)
at android.view.View.draw(View.java:14433)
at android.view.View.draw(View.java:14318)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14316)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14316)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14436)
at android.view.View.draw(View.java:14318)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14436)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:584)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:338)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:334)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:451)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1545)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1302)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:1059)
at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.delegateActivated(LayoutEditorDelegate.java:743)
at com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor.activated(CommonXmlEditor.java:416)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.LayoutWindowCoordinator.partActivated(LayoutWindowCoordinator.java:379)
at org.eclipse.ui.internal.PartService$6.run(PartService.java:131)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.ui.internal.PartService.partActivated(PartService.java:129)
at org.eclipse.ui.internal.WorkbenchPage$12.run(WorkbenchPage.java:4462)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.ui.internal.WorkbenchPage.firePartActivated(WorkbenchPage.java:4460)
at org.eclipse.ui.internal.WorkbenchPage.access$16(WorkbenchPage.java:4440)
at org.eclipse.ui.internal.WorkbenchPage$E4PartListener.partActivated(WorkbenchPage.java:173)
at org.eclipse.e4.ui.internal.workbench.PartServiceImpl$2.run(PartServiceImpl.java:193)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.firePartActivated(PartServiceImpl.java:191)
at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.activate(PartServiceImpl.java:596)
at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.activate(PartServiceImpl.java:549)
at org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer.activate(AbstractPartRenderer.java:105)
at org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer$ActivationJob.run(StackRenderer.java:214)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4144)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)

1 个答案:

答案 0 :(得分:0)

我终于想出了如何更新TextViews。我这样做的方法是在绘图类中创建两个TextView对象,如下所示:

         TextView v1 = (TextView) this.getRootView().findViewById(R.id.textView1);
         TextView v2 = (TextView) this.getRootView().findViewById(R.id.textView2);

getRootView关键字是我从一个绘图所在的View中获取一些数据对象所需要的。我让程序通过设置X和Y TextViews来更新值:

  protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);   
    try{            
         TextView v1 = (TextView) this.getRootView().findViewById(R.id.textView1);
         TextView v2 = (TextView) this.getRootView().findViewById(R.id.textView2);
         v1.setText("X: "+eventX);
         v2.setText("Y: "+eventY);
        }
        catch(Exception e1){
            Log.d("myError", e1);
        }
  }