问题
I made a button with a pressed effect with selector.. I am using android studio, when I use eclipse its running well.. but, when I try in android studio, the log keeps displaying error..
This is my log:
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.widget.ImageView.<init>(ImageView.java:139)
at android.widget.ImageButton.<init>(ImageButton.java:86)
at android.widget.ImageButton.<init>(ImageButton.java:82)
at android.widget.ImageButton.<init>(ImageButton.java:78)
at sun.reflect.GeneratedConstructorAccessor75.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.rInflate_Original(LayoutInflater.java:806)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
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:688)
at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:677)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:677)
at com.android.tools.idea.rendering.RenderService.render(RenderService.java:815)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.
doRender(AndroidLayoutPreviewToolWindowManager.java:646)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.
access$1700(AndroidLayoutPreviewToolWindowManager.java:82)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.
run(AndroidLayoutPreviewToolWindowManager.java:589)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress
(ProgressManager.java:209)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress
(ProgressManagerImpl.java:212)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess
(ProgressManagerImpl.java:171)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run
(AndroidLayoutPreviewToolWindowManager.java:584)
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)
This is my selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/press_pdgmap" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@drawable/hover_pdgmap2" /> <!-- hover -->
<item android:drawable="@drawable/hover_pdgmap" /> <!-- default -->
</selector>
In my activity I use :
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/selector"
android:clickable="true" />
In my activity a red marker keeps showing in "android:background="@drawable/selector" line.
I have move image and selector file di drawable, what am I doing wrong?
Note: sorry if my explanation is bad.
回答1:
I got this same behavior with Android Studio. It appears that the problem is that Android studio new file option doesn't add file extension to xml files if you don't call the file name.xml. Just make sure that your selector xmls under \res\drawable\ really have .xml extension.
NOTE: It also appears that if they don't have required extension auto complete (for image files) will not work but it will not show any errors if you specify full file name and path (also if you paste text content from somewhere errors will not show). Auto complete will also not work when you set selector to button in layout but if again you specify full name it will just show that red circle with x but not tell you what the problem is.
回答2:
As per stack-trace the issue is in the image button. Try to clean and build the project and also see that the image you are using in selector is in valid format or not if its not it will have a problem in rendering the imagebutton that can cause the error.
回答3:
I change code for custom selector for button into this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@drawable/press" />
</item>
<item android:state_focused="true">
<bitmap android:src="@drawable/notpress" />
</item>
<item>
<bitmap android:src="@drawable/notpress" />
</item>
</selector>
And it's work you can see my android studio pic
NB: i'm Using Android Studio v1.0.1 and Update my SDK into latest version
回答4:
This works for my app. Try this one.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/myGrey"
android:state_pressed="true" />
<item android:drawable="@color/myBlack"/>
</selector>
来源:https://stackoverflow.com/questions/27469832/image-button-with-selector-in-android-studio-error-rendering