2011-09-19 27 views
1

我正在嘗試創建繪製位圖的簡單自定義組件。當我以編程方式添加它時它工作正常。在xml錯誤中添加自定義組件

RelativeLayout layout = (RelativeLayout) findViewById(R.id.linlay); Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), 
       R.drawable.girl); 

AdView ad = new AdView(this,mBitmap); 

layout.addView(ad); 

但是當我嘗試通過XML文件中添加它,我得到錯誤:

<test.com.ad.AdView 
    android:id="@+id/ad" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    ></test.com.ad.AdView> 

java.lang.NullPointerException at test.com.ad.AdView.onMeasure(AdView.java:81) at android.view.View.measure(View.java:10577) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365) at android.view.View.measure(View.java:10577) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4270) at android.widget.FrameLayout.onMeasure(FrameLayout.java:267) at android.view.View.measure(View.java:10577) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4270) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284) at android.widget.LinearLayout.measureVertical(LinearLayout.java:613) at android.widget.LinearLayout.onMeasure(LinearLayout.java:519) at android.view.View.measure(View.java:10577) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4270) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284) at android.widget.LinearLayout.measureVertical(LinearLayout.java:613) at android.widget.LinearLayout.onMeasure(LinearLayout.java:519) at android.view.View.measure(View.java:10577) at com.android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.java:551) at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:453) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:323) 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$2.widgetSelected(MultiPageEditorPart.java:290) at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:240) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4125) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1457) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1480) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1465) at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1270) at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:2745) at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1432) at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:257) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4125) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1457) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1480) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1465) at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1270) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3971) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3610) at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696) at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660) at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494) at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123) 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:344) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577) at org.eclipse.equinox.launcher.Main.run(Main.java:1410)

我onMeasure看起來像這樣:

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     // TODO Auto-generated method stub 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     setMeasuredDimension(resolveSize(bitmap.getWidth(), widthMeasureSpec), 
       resolveSize(bitmap.getHeight(), heightMeasureSpec)); 

    } 

這裏有什麼問題?

+0

位圖的價值是什麼?它沒有獲得位圖,因此沒有獲得NPE。 – PravinCG

回答

2

當你在編碼,那麼你路過mBitmap但是當你試圖通過XML來添加它然後mBitmap爲空監守你沒有通過mBitmap ..所以你必須改變你的AdView(this)代替AdView(this,mBitmap);AdView()添加 Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.girl); 調用new AdView(this,mBitmap);

相關問題