设置imageView的宽度和高度以编程方式引发异常

时间:2014-05-22 09:59:52

标签: android imageview height width

我试图以编程方式设置图片,但是当我设置它的大小时:widht和height它会引发异常。

PS:我正在使用ImageLayout Library

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
       imageLayout = (ImageLayout)findViewById(R.id.image_layout);

   ImageView image = new ImageView(this);
   image.setImageResource(R.drawable.positioning);
   ImageLayout.LayoutParams layoutParams2 = new ImageLayout.LayoutParams(); 
   image.requestLayout();
   image.getLayoutParams().height = 100;
   image.getLayoutParams().width = 100;
    layoutParams2.left = 243;
    layoutParams2.top = 297;
    layoutParams2.right = 432;
    layoutParams2.bottom = 405;
    imageLayout.addView(image, layoutParams2);
 }

XML Layou t:

    <?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>

LogCat(第25行:image.getLayoutParams()。height = 100;)

    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

1 个答案:

答案 0 :(得分:0)

这是一个NPE,因为尚未绘制视图,并且没有与之关联的布局参数。你应该首先绘制布局,然后才能获得布局参数。

相关问题