why the SAMPLE1 code is working fine and SAMPLE2 throws NullPointerException?
do getWindowManager()
work only inside onCreate?what can i do to create a method that will return height of the view and to make it available for all subclasses? please help..
SAMPLE1
public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
final int height = displaymetrics.heightPixels;
float imheight= (float) (height*.4);
int h=Math.round(imheight);
ImageView im=(ImageView)findViewById(R.id.mainnews1);
im.getLayoutParams().height = h;
im.setScaleType(ImageView.ScaleType.FIT_XY);
}
SAMPLE2
public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView im=(ImageView)findViewById(R.id.mainnews1);
im.getLayoutParams().height = new MainActivity().heightof();
im.setScaleType(ImageView.ScaleType.FIT_XY);
}
public int heightof()
{
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
final int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
float imheight= (float) (height*.4);
int h=Math.round(imheight);
return h;
}
}