在android studio中,如果我将.xml drawable设置为背景,我会得到渲染问题,并且在运行app时 - 看不到背景。
例如,我无法将此xml设置为布局背景。
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/bad" />
如果我添加,例如,
android:tileMode="repeat"
结果是一样的。我还在背景附近看到红色感叹号:
这是堆栈跟踪:
java.lang.NullPointerException
at android.graphics.Bitmap_Delegate.createBitmap(Bitmap_Delegate.java:614)
at android.graphics.Bitmap_Delegate.createBitmap(Bitmap_Delegate.java:122)
at android.graphics.Bitmap_Delegate.createBitmap(Bitmap_Delegate.java:104)
at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:249)
at android.content.res.BridgeTypedArray.getDrawable(BridgeTypedArray.java:674)
at android.view.View.<init>(View.java:3730)
at android.view.ViewGroup.<init>(ViewGroup.java:491)
at android.widget.LinearLayout.<init>(LinearLayout.java:200)
at android.widget.LinearLayout.<init>(LinearLayout.java:196)
at android.widget.LinearLayout.<init>(LinearLayout.java:192)
at sun.reflect.GeneratedConstructorAccessor302.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.BridgeInflater.onCreateView(BridgeInflater.java:82)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:128)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:400)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:332)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:708)
at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:697)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:697)
at com.android.tools.idea.rendering.RenderService.render(RenderService.java:816)
at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$6.run(AndroidDesignerEditorPanel.java:480)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:327)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:5)
我喜欢这个解决方案。这很简单。
我需要将app_background
重命名为app_background.xml
Android工作室创建了文件&#34; app_background&#34;默认情况下没有&#34; .xml&#34;推广。我认为Android工作室隐藏了.xml,但当我在资源管理器中打开文件夹时,我意识到文件的真实名称
答案 1 :(得分:0)
请将图像(Ex.bad.png)复制到drawable文件夹中。 然后在drawable文件夹中创建background.xml并粘贴到代码
下面 <?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/bad"
android:tileMode="repeat" />
然后在你的布局设置背景为android:background =&#34; @ drawable / background&#34;