gpt4 book ai didi

android - 在 xml 错误中添加自定义组件

转载 作者:行者123 更新时间:2023-11-29 22:20:03 25 4
gpt4 key购买 nike

我正在尝试创建绘制位图的简单自定义组件。当我以编程方式添加它时,它工作正常。

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));

}

这里有什么问题吗?

最佳答案

当您在编码时调用 new AdView(this,mBitmap); 时,您传递了 mBitmap 但是当您尝试通过 xml 添加它时,mBitmap 为空,因为您没有通过 mBitmap.. 所以你必须改变你的 AdView(this) 而不是 AdView(this,mBitmap); 并添加位图 mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.girl);AdView() 类中

关于android - 在 xml 错误中添加自定义组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7470724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com