图库演示 NullPointerException

发布于 2024-09-16 02:34:06 字数 7073 浏览 8 评论 0原文

http://developer.android.com/guide/tutorials/views/ hello-gallery.html

我完全按照指南进行操作,我可以编译并运行该程序,但它在加载时崩溃。

当我尝试查看 main.xml 时,控制台中出现错误。

java.lang.NullPointerException
 at android.view.GestureDetector.<init>(GestureDetector.java:347)
 at android.view.GestureDetector.<init>(GestureDetector.java:331)
 at android.widget.Gallery.<init>(Gallery.java:181)
 at android.widget.Gallery.<init>(Gallery.java:175)
 at sun.reflect.GeneratedConstructorAccessor38.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at android.view.LayoutInflater.createView(LayoutInflater.java:500)
 at android.view.BridgeInflater.onCreateView(BridgeInflater.java:77)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
 at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:122)
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:296)
 at com.android.layoutlib.bridge.Bridge.computeLayout(Bridge.java:397)
 at com.android.ide.eclipse.adt.internal.editors.layout.gle1.GraphicalLayoutEditor.computeLayout(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.layout.gle1.GraphicalLayoutEditor.recomputeLayout(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.layout.gle1.GraphicalLayoutEditor.onXmlModelChanged(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.xmlModelChanged(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.AndroidEditor$XmlModelStateListener.modelChanged(Unknown Source)
 at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.fireModelChanged(AbstractStructuredModel.java:553)
 at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.internalModelChanged(AbstractStructuredModel.java:887)
 at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.changedModel(AbstractStructuredModel.java:382)
 at org.eclipse.wst.xml.core.internal.document.DOMModelImpl.changedModel(DOMModelImpl.java:167)
 at org.eclipse.wst.sse.ui.StructuredTextEditor.savedModel(StructuredTextEditor.java:2814)
 at org.eclipse.wst.sse.ui.StructuredTextEditor.doSave(StructuredTextEditor.java:1826)
 at com.android.ide.eclipse.adt.internal.editors.AndroidEditor.doSave(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.doSave(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.showEditorInput(Unknown Source)
 at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditorBatched(WorkbenchPage.java:2847)
 at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditor(WorkbenchPage.java:2768)
 at org.eclipse.ui.internal.WorkbenchPage.access$11(WorkbenchPage.java:2760)
 at org.eclipse.ui.internal.WorkbenchPage$10.run(WorkbenchPage.java:2711)
 at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
 at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2707)
 at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2691)
 at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2682)
 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:365)
 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:845)
 at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
 at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:49)
 at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
 at org.eclipse.jface.viewers.StructuredViewer.fireOpen(StructuredViewer.java:843)
 at org.eclipse.jface.viewers.StructuredViewer.handleOpen(StructuredViewer.java:1131)
 at org.eclipse.jface.viewers.StructuredViewer$6.handleOpen(StructuredViewer.java:1235)
 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:1053)
 at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
 at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
 at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2629)
 at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2593)
 at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2427)
 at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:670)
 at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
 at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:663)
 at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
 at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
 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:369)
 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:619)
 at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
 at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
 at org.eclipse.equinox.launcher.Main.main(Main.java:1383)

编辑:实际上,如果我创建一个新项目并添加一个 Gallery,我仍然会收到 NullPointerException。我认为这不应该是默认行为。

http://developer.android.com/guide/tutorials/views/hello-gallery.html

I followed the guide exactly, I can compile and run the program but it crashes on load.

When I try to view main.xml in I get errors in the Console.

java.lang.NullPointerException
 at android.view.GestureDetector.<init>(GestureDetector.java:347)
 at android.view.GestureDetector.<init>(GestureDetector.java:331)
 at android.widget.Gallery.<init>(Gallery.java:181)
 at android.widget.Gallery.<init>(Gallery.java:175)
 at sun.reflect.GeneratedConstructorAccessor38.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at android.view.LayoutInflater.createView(LayoutInflater.java:500)
 at android.view.BridgeInflater.onCreateView(BridgeInflater.java:77)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
 at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:122)
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:296)
 at com.android.layoutlib.bridge.Bridge.computeLayout(Bridge.java:397)
 at com.android.ide.eclipse.adt.internal.editors.layout.gle1.GraphicalLayoutEditor.computeLayout(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.layout.gle1.GraphicalLayoutEditor.recomputeLayout(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.layout.gle1.GraphicalLayoutEditor.onXmlModelChanged(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.xmlModelChanged(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.AndroidEditor$XmlModelStateListener.modelChanged(Unknown Source)
 at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.fireModelChanged(AbstractStructuredModel.java:553)
 at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.internalModelChanged(AbstractStructuredModel.java:887)
 at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.changedModel(AbstractStructuredModel.java:382)
 at org.eclipse.wst.xml.core.internal.document.DOMModelImpl.changedModel(DOMModelImpl.java:167)
 at org.eclipse.wst.sse.ui.StructuredTextEditor.savedModel(StructuredTextEditor.java:2814)
 at org.eclipse.wst.sse.ui.StructuredTextEditor.doSave(StructuredTextEditor.java:1826)
 at com.android.ide.eclipse.adt.internal.editors.AndroidEditor.doSave(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.doSave(Unknown Source)
 at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.showEditorInput(Unknown Source)
 at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditorBatched(WorkbenchPage.java:2847)
 at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditor(WorkbenchPage.java:2768)
 at org.eclipse.ui.internal.WorkbenchPage.access$11(WorkbenchPage.java:2760)
 at org.eclipse.ui.internal.WorkbenchPage$10.run(WorkbenchPage.java:2711)
 at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
 at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2707)
 at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2691)
 at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2682)
 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:365)
 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:845)
 at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
 at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:49)
 at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
 at org.eclipse.jface.viewers.StructuredViewer.fireOpen(StructuredViewer.java:843)
 at org.eclipse.jface.viewers.StructuredViewer.handleOpen(StructuredViewer.java:1131)
 at org.eclipse.jface.viewers.StructuredViewer$6.handleOpen(StructuredViewer.java:1235)
 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:1053)
 at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
 at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
 at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2629)
 at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2593)
 at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2427)
 at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:670)
 at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
 at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:663)
 at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
 at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
 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:369)
 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:619)
 at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
 at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
 at org.eclipse.equinox.launcher.Main.main(Main.java:1383)

EDIT: Actually, If I make a new project and just add a Gallery I still get the NullPointerException. I don't think that should be the default behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开半夏魅人心 2024-09-23 02:34:06

是的,我的图像中有 a、b、c、d、e
res/drawable-ldpi 文件夹。然后我有
代码如下:R.drawable.a 等 –
user157603 35 分钟前

不符合示例,您必须拥有名为

****sample_1.png、sample_2.png、sample_3.png、sample_4.png、sample_5.png、sample_6.png、sample_7.png** 的图像
(或.jpg)
在你的 res/drawable 文件夹中。

否则你会得到一个 NullPointerException

yes, I have images a, b, c, d, e in my
res/drawable-ldpi folder. Then I have
the code like this: R.drawable.a etc –
user157603 35 mins ago

no according with the example, you must have the images called

**** sample_1.png,sample_2.png,sample_3.png,sample_4.png,sample_5.png,sample_6.png,sample_7.png**
(or .jpg)
inside your res/drawable folder.

or you will you get a NullPointerException

我乃一代侩神 2024-09-23 02:34:06

您是否正确命名了可绘制目录中的图像?

源代码使用 R.drawable.sample_1 (等等),因此您的图像应命名为 sample_1.png (等等)。文件扩展名也可以是 jpg、gif...,具体取决于您使用的图像格式

have you named your images in the drawable directory properly?

the source code uses R.drawable.sample_1 (and so on) so your images should be named sample_1.png (and so on). file extension could also be jpg, gif... depending on the image format you use

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文