2014-01-21 51 views
0

我最近一直在研究Android應用程序,它是一個簡單的XY-Pad,它將在設置View中顯示用戶輸入的X和Y座標。它的工作方式是爲應用程序提供一個XML佈局,該佈局具有兩個TextView(一個顯示X值,一個顯示Y值),另外還有一個View,即XY-Pad所在的View。但是我仍然遇到的問題是,當我嘗試將X和Y座標發送到XML的鏈接類文件以設置TextView的文本時,出現錯誤(NullPointerException錯誤)。有沒有人知道通過代碼可能會出錯?XY-Pad得到X和Y NPE錯誤(已解決)

這裏是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" /> 

這裏是 '畫' 類,這是去查看在XML數據的內部:

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) 
+0

您可以發佈NPE嗎? – Thierry

+0

我把上面的異常堆棧跟蹤,如果這是你想要的... ... – Greg

回答

0

我終於想出瞭如何更新TextViews。我做的方式,它是通過創建繪圖類似於是兩個TextView的對象:

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

的getRootView關鍵字是什麼,我需要得到一些數據對象了查看圖紙是在我製作的節目通過設置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); 
     } 
    } 
相關問題