Android 通过编程设置imageView宽度和高度引发异常

Android 通过编程设置imageView宽度和高度引发异常,android,imageview,height,width,Android,Imageview,Height,Width,我试图通过编程设置图像,但当我设置图像的大小:宽度和高度时,会引发一个异常 PS:Im使用ImageLayout库 XML布局: <?xml version="1.0" encoding="utf-8" ?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-a

我试图通过编程设置图像,但当我设置图像的大小:宽度和高度时,会引发一个异常

PS:Im使用ImageLayout库

XML布局:

    <?xml version="1.0" encoding="utf-8" ?> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent">

<com.manuelpeinado.imagelayout.ImageLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/image_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#aaa"
    custom:fit="both"
    custom:image="@drawable/boroughs2"
    custom:imageHeight="1744"
    custom:imageWidth="1150" >

  <Button android:id="@+id/manhattan" 
      style="@style/ImageText" 
      custom:layout_centerX="608" 
      custom:layout_centerY="595" 
      android:text="@string/manhattan" 
      android:onClick="onButtonClick" />

  <ImageView
      android:id="@+id/imageView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/red" 
      custom:layout_centerX="160" 
      custom:layout_centerY="200" />

  <TextView
      android:id="@+id/router1"
      style="@style/ImageText.Normal"
      custom:layout_centerX="160"
      custom:layout_centerY="64"
      custom:layout_maxWidth="440"
      android:text="Router 1"
      android:textColor="#FF0000" />

  <ImageView
      android:id="@+id/imageView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/red" 
      custom:layout_centerX="1000" 
      custom:layout_centerY="640" />

  <TextView
      android:id="@+id/router2"
      style="@style/ImageText.Normal"
      custom:layout_centerX="1000"
      custom:layout_centerY="500"
      custom:layout_maxWidth="440"
      android:text="Router 2"
      android:textColor="#FF0000" />


  <ImageView
      android:id="@+id/imageView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/red" 
      custom:layout_centerX="300" 
      custom:layout_centerY="1500" />

  <TextView
      android:id="@+id/router3"
      style="@style/ImageText.Normal"
      custom:layout_centerX="300"
      custom:layout_centerY="1400"
      custom:layout_maxWidth="440"
      android:text="Router 3"
      android:textColor="#FF0000" />

  </com.manuelpeinado.imagelayout.ImageLayout>
   </HorizontalScrollView>

它是NPE,因为尚未绘制视图,并且没有与其关联的布局参数。您应该先绘制布局,然后才可以获得布局参数。

谢谢您的回复。但是,如何使用该代码首先绘制布局@阿吉特·普拉塔普·辛格
    <?xml version="1.0" encoding="utf-8" ?> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent">

<com.manuelpeinado.imagelayout.ImageLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/image_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#aaa"
    custom:fit="both"
    custom:image="@drawable/boroughs2"
    custom:imageHeight="1744"
    custom:imageWidth="1150" >

  <Button android:id="@+id/manhattan" 
      style="@style/ImageText" 
      custom:layout_centerX="608" 
      custom:layout_centerY="595" 
      android:text="@string/manhattan" 
      android:onClick="onButtonClick" />

  <ImageView
      android:id="@+id/imageView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/red" 
      custom:layout_centerX="160" 
      custom:layout_centerY="200" />

  <TextView
      android:id="@+id/router1"
      style="@style/ImageText.Normal"
      custom:layout_centerX="160"
      custom:layout_centerY="64"
      custom:layout_maxWidth="440"
      android:text="Router 1"
      android:textColor="#FF0000" />

  <ImageView
      android:id="@+id/imageView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/red" 
      custom:layout_centerX="1000" 
      custom:layout_centerY="640" />

  <TextView
      android:id="@+id/router2"
      style="@style/ImageText.Normal"
      custom:layout_centerX="1000"
      custom:layout_centerY="500"
      custom:layout_maxWidth="440"
      android:text="Router 2"
      android:textColor="#FF0000" />


  <ImageView
      android:id="@+id/imageView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/red" 
      custom:layout_centerX="300" 
      custom:layout_centerY="1500" />

  <TextView
      android:id="@+id/router3"
      style="@style/ImageText.Normal"
      custom:layout_centerX="300"
      custom:layout_centerY="1400"
      custom:layout_maxWidth="440"
      android:text="Router 3"
      android:textColor="#FF0000" />

  </com.manuelpeinado.imagelayout.ImageLayout>
   </HorizontalScrollView>
    05-22 10:57:26.206: W/asset(30765): AssetManager-->addDefaultAssets CIP path not exsit!
05-22 10:57:27.190: I/dalvikvm-heap(30765): Grow heap (frag case) to 7.201MB for 4515232-byte allocation
05-22 10:57:27.809: I/dalvikvm-heap(30765): Grow heap (frag case) to 9.112MB for 2005616-byte allocation
05-22 10:57:27.971: W/dalvikvm(30765): threadid=1: thread exiting with uncaught exception (group=0x40cb39a8)
05-22 10:57:27.987: E/AndroidRuntime(30765): FATAL EXCEPTION: main
05-22 10:57:27.987: E/AndroidRuntime(30765): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drawdraw/com.example.drawdraw.MainActivity}: java.lang.NullPointerException
05-22 10:57:27.987: E/AndroidRuntime(30765):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at android.os.Handler.dispatchMessage(Handler.java:107)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at android.os.Looper.loop(Looper.java:194)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at android.app.ActivityThread.main(ActivityThread.java:5371)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at java.lang.reflect.Method.invoke(Method.java:525)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at dalvik.system.NativeStart.main(Native Method)
05-22 10:57:27.987: E/AndroidRuntime(30765): Caused by: java.lang.NullPointerException
05-22 10:57:27.987: E/AndroidRuntime(30765):    at com.example.drawdraw.MainActivity.onCreate(MainActivity.java:25)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at android.app.Activity.performCreate(Activity.java:5122)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-22 10:57:27.987: E/AndroidRuntime(30765):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
05-22 10:57:27.987: E/AndroidRuntime(30765):    ... 11 more