Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7956803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:06:08+00:00 2026-06-04T04:06:08+00:00

Hi I wish to change my image height and width depending upon the screen

  • 0

Hi I wish to change my image height and width depending upon the screen resolution so i am using below code but i am getting error in logcat:

1st my source code :

 public class ImageviewAppActivity extends Activity {

    private ImageView img; 
    private LayoutParams params;
    private int width, height;
    /** Called when the activity is first created. */
    @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        height=160; //getWindowManager().getDefaultDisplay().getHeight();
        width = 160; //getWindowManager().getDefaultDisplay().getWidth();

        params = new LayoutParams(width, height);
        img.setLayoutParams(params);

        img =(ImageView) findViewById(R.id.imagetest);
        img.setImageResource(R.drawable.last);
   }}

logcat error:

emulator error:

05-19 20:02:08.689: D/AndroidRuntime(1933): Shutting down VM
05-19 20:02:08.689: W/dalvikvm(1933): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-19 20:02:08.749: E/AndroidRuntime(1933): FATAL EXCEPTION: main
05-19 20:02:08.749: E/AndroidRuntime(1933): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.ImageviewAppActivity}: java.lang.NullPointerException
05-19 20:02:08.749: E/AndroidRuntime(1933):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at android.os.Looper.loop(Looper.java:123)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at java.lang.reflect.Method.invokeNative(Native Method)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at java.lang.reflect.Method.invoke(Method.java:507)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at dalvik.system.NativeStart.main(Native Method)
05-19 20:02:08.749: E/AndroidRuntime(1933): Caused by: java.lang.NullPointerException
05-19 20:02:08.749: E/AndroidRuntime(1933):     at com.test.ImageviewAppActivity.onCreate(ImageviewAppActivity.java:46)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-19 20:02:08.749: E/AndroidRuntime(1933):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-19 20:02:08.749: E/AndroidRuntime(1933):     ... 11 more
05-19 20:02:08.779: W/ActivityManager(75):   Force finishing activity com.test/.ImageviewAppActivity
05-19 20:02:09.329: W/ActivityManager(75): Activity pause timeout for HistoryRecord{40970fc8 com.test/.ImageviewAppActivity}
05-19 20:02:17.199: D/dalvikvm(378): GC_EXPLICIT freed 7K, 54% free 2537K/5511K, external 1625K/2137K, paused 171ms
05-19 20:02:21.880: W/ActivityManager(75): Activity destroy timeout for HistoryRecord{40970fc8 com.test/.ImageviewAppActivity}

The application ImageviewApp(process com.test.app)has stopped unexpectedly . please try again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:06:11+00:00Added an answer on June 4, 2026 at 4:06 am

    img.setLayoutParams(params);

    put above line after this line

    img =(ImageView) findViewById(R.id.imagetest);

    AND

    it your LayoutParams problem, change it check imported package for layourparams and change it…and i dont know which viewgroup you have …but check belows package use one…

    import android.widget.RelativeLayout.LayoutParams;
    

    or

    import android.view.ViewGroup.LayoutParams;     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to change this following assembly code into UNIX compatible code without using
I use the following code to reload an image using jQuery: //Fetch image associated
I wish to change the width of an element specifically only in IE browsers
I am using Tim Pope's Fugitive plugin and I wish to change the :Gstatus
I am using a jquery to show fields of HTML like select,text, image. But
I have an image that is 320x480 and upon orientation change this image obviously
I wish to replace spaces in something like this: <span>5</span>SPACE<image href=mtgsymbol.png /> but when
As the title explains, I wish to change the color of a row when
I have a System.Type stored in a variable. I wish to Change an object's
I wish to send an email from my localhost machine (using PHPs mail function)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.