2016-03-12 118 views
0

當我嘗試通過代碼設置ImageView的剪輯級別時,我的ClipDrawable引發異常。 Logcat指向img.setLevel(...)旁邊的其他人。我的代碼有什麼問題?佈局ClipDrawable不工作

clip.xml

<?xml version="1.0" encoding="utf-8"?> 
<clip 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/image" 
    android:clipOrientation="horizontal" 
    android:gravity="left" /> 

ImageView的佈局

<ImageView 
      android:id="@+id/imageview1" 
      android:background="@drawable/clip" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

代碼(之後的setContentView(佈局))

ImageView img = (ImageView) findViewById(R.id.imageview1); 
     ClipDrawable drawable = (ClipDrawable) img.getDrawable(); 
     img.setLevel(drawable.getLevel() + 1000); 

編輯:登錄:

FATAL EXCEPTION: main 
    java.lang.IllegalStateException: Could not execute method of the activity 
      at android.view.View$1.onClick(View.java:2144) 
      at android.view.View.performClick(View.java:2485) 
      at android.view.View$PerformClick.run(View.java:9080) 
      at android.os.Handler.handleCallback(Handler.java:587) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:130) 
      at android.app.ActivityThread.main(ActivityThread.java:3687) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:507) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:507) 
      at android.view.View$1.onClick(View.java:2139) 
            at android.view.View.performClick(View.java:2485) 
            at android.view.View$PerformClick.run(View.java:9080) 
            at android.os.Handler.handleCallback(Handler.java:587) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:130) 
            at android.app.ActivityThread.main(ActivityThread.java:3687) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:507) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at game.gam.Game.prepniNaLevelLayout(Game.java:356) 
      at game.gam.Game.onImageButtonClick(Game.java:163) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:507) 
            at android.view.View$1.onClick(View.java:2139) 
            at android.view.View.performClick(View.java:2485) 
            at android.view.View$PerformClick.run(View.java:9080) 
            at android.os.Handler.handleCallback(Handler.java:587) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:130) 
            at android.app.ActivityThread.main(ActivityThread.java:3687) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:507) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
            at dalvik.system.NativeStart.main(Native Method) 
+0

總是顯示異常的完整堆棧跟蹤。 –

+0

我已編輯主帖。 – johnyX

+0

你已經完成了一步調試?它看起來像你的drawable是null。 –

回答

0

老問題,但發現它尋找解決不同的問題。 OP的問題是他們試圖設置ImageView的背景而不是源代碼。根據我的經驗,image.getDrawable()正在尋找android:src標籤,這就是爲什麼OP得到空值。應該是:

<ImageView 
     android:id="@+id/imageview1" 
     android:src="@drawable/clip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" />