访问图形布局时抛出空指针异常

时间:2011-08-29 11:20:21

标签: android nullpointerexception

代码: -

public class DigitalDial extends View{
    private Paint titlePaint;
    private String title;
    private Float value ;

    public DigitalDial(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public DigitalDial(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DigitalDial(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setValue(float value) {
        this.value = value;
        invalidate();   
    }

@Override 
protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // draw some text using FILL style
        titlePaint = new Paint();
        titlePaint.setStyle(Paint.Style.FILL);
        titlePaint.setTypeface(Typeface.DEFAULT_BOLD);
        //paint.setTextAlign(Paint.Align.RIGHT);
        titlePaint.setColor(Color.YELLOW);
        titlePaint.setAntiAlias(true);
        titlePaint.setTextSize(28);
        drawTitle(canvas);

    }   

    public void setTitle(String title) {
        this.title = title;
    }

    private String getTitle() {
        return title;
    }

    public void drawTitle(Canvas canvas) {
        String title = getTitle();
        canvas.drawText(title,30, 48, titlePaint);              
    }
}

访问图形布局时出错: - java.lang.NullPointerException

调试时显示错误:titlePaint为null,但为什么????

帮助表示赞赏。

抱歉代码不完整。缺少代码段; -

    package pkg.digitalDial;

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.CountDownTimer;

    import pkg.digitalDial.DigitalDial;

    public class DigitalDialActivity extends Activity {
           /** Called when the activity is first created. */
        DigitalDial speedDial ;
        CountDownTimer speedTimer;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.digitaldial);          
                speedDial = (DigitalDial) findViewById(R.id.dial1);

                speedDial.setTitle("Speed");
                speedDial.setValue(2.0f);
            }            
    }

错误日志: -

java.lang.NullPointerException
    at android.graphics.Canvas_Delegate.drawText(Canvas_Delegate.java:276)
    at android.graphics.Canvas.drawText(Canvas.java)
    at pkg.digitalDial.DigitalDial.drawTitle(DigitalDial.java:62)
    at pkg.digitalDial.DigitalDial.onDraw(DigitalDial.java:48)
    at android.view.View.draw(View.java:6740)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    at android.view.View.draw(View.java:6743)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    at android.view.View.draw(View.java:6743)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:466)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:320)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:380)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1310)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1075)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:901)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.pageChange(LayoutEditor.java:403)
    at org.eclipse.ui.part.MultiPageEditorPart.setActivePage(MultiPageEditorPart.java:1076)
    at org.eclipse.ui.forms.editor.FormEditor.setActivePage(FormEditor.java:601)
    at com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor.selectDefaultPage(AndroidXmlEditor.java:314)
    at com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor.addPages(AndroidXmlEditor.java:219)
    at org.eclipse.ui.forms.editor.FormEditor.createPages(FormEditor.java:138)
    at org.eclipse.ui.part.MultiPageEditorPart.createPartControl(MultiPageEditorPart.java:357)
    at org.eclipse.ui.internal.EditorReference.createPartHelper(EditorReference.java:662)
    at org.eclipse.ui.internal.EditorReference.createPart(EditorReference.java:462)
    at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:595)
    at org.eclipse.ui.internal.EditorReference.getEditor(EditorReference.java:286)
    at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditorBatched(WorkbenchPage.java:2857)
    at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditor(WorkbenchPage.java:2762)
    at org.eclipse.ui.internal.WorkbenchPage.access$11(WorkbenchPage.java:2754)
    at org.eclipse.ui.internal.WorkbenchPage$10.run(WorkbenchPage.java:2705)
    at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
    at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2701)
    at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2685)
    at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2676)
    at org.eclipse.ui.ide.IDE.openEditor(IDE.java:651)
    at org.eclipse.ui.ide.IDE.openEditor(IDE.java:610)
    at org.eclipse.jdt.internal.ui.javaeditor.EditorUtility.openInEditor(EditorUtility.java:361)
    at org.eclipse.jdt.internal.ui.javaeditor.EditorUtility.openInEditor(EditorUtility.java:168)
    at org.eclipse.jdt.ui.actions.OpenAction.run(OpenAction.java:229)
    at org.eclipse.jdt.ui.actions.OpenAction.run(OpenAction.java:208)
    at org.eclipse.jdt.ui.actions.SelectionDispatchAction.dispatchRun(SelectionDispatchAction.java:274)
    at org.eclipse.jdt.ui.actions.SelectionDispatchAction.run(SelectionDispatchAction.java:250)
    at org.eclipse.jdt.internal.ui.packageview.PackageExplorerActionGroup.handleOpen(PackageExplorerActionGroup.java:373)
    at org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart$4.open(PackageExplorerPart.java:526)
    at org.eclipse.ui.OpenAndLinkWithEditorHelper$InternalListener.open(OpenAndLinkWithEditorHelper.java:48)
    at org.eclipse.jface.viewers.StructuredViewer$2.run(StructuredViewer.java:842)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.runtime.Platform.run(Platform.java:888)
    at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
    at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
    at org.eclipse.jface.viewers.StructuredViewer.fireOpen(StructuredViewer.java:840)
    at org.eclipse.jface.viewers.StructuredViewer.handleOpen(StructuredViewer.java:1101)
    at org.eclipse.jface.viewers.StructuredViewer$6.handleOpen(StructuredViewer.java:1205)
    at org.eclipse.jface.util.OpenStrategy.fireOpenEvent(OpenStrategy.java:264)
    at org.eclipse.jface.util.OpenStrategy.access$2(OpenStrategy.java:258)
    at org.eclipse.jface.util.OpenStrategy$1.handleEvent(OpenStrategy.java:298)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3880)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3473)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2405)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2369)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2221)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:500)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:493)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194)
    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:368)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    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:559)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1311)

2 个答案:

答案 0 :(得分:1)

当您寻求有关异常的帮助时,发布完整堆栈跟踪始终是个好主意。这样,我们就可以更容易地找出确切的问题!

如果没有,我们只能猜测,但如果这是您的所有代码,我猜NullPointerException来自title字段。这里:

public void drawTitle(Canvas canvas) {
    String title = getTitle();
    canvas.drawText(title,30, 48, titlePaint);              
}

您正在使用title的值,我没有看到它在任何地方初始化。 String引用很可能是null

答案 1 :(得分:1)

您需要将绘画设置移动到构造函数中:

public DigitalDial(Context context) {         
  super(context);         

  titlePaint = new Paint();         
  titlePaint.setStyle(Paint.Style.FILL);         
  titlePaint.setTypeface(Typeface.DEFAULT_BOLD);         
  //paint.setTextAlign(Paint.Align.RIGHT);         
  titlePaint.setColor(Color.YELLOW);         
  titlePaint.setAntiAlias(true);         
  titlePaint.setTextSize(28); 

} 

您正在使用每次绘制创建一个新的绘制对象,这对效率非常不利。您也不需要重载构造函数,因为您知道如何实例化自定义视图,并且您不太可能在应用程序中以三种不同的方式执行此操作。

在第一次绘制瓷砖时,圣地亚哥是正确的,因此你应该在顶部说:

  

private String title =“”;