0

If I start app in portrait mode , it works fine.

But if I start it into landscape mode then it gives nullPointerException when I try to resize my view by using setLayoutParams.

piece of code

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.companygraph);
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
                .getDefaultDisplay();
        if (display.getWidth() > display.getHeight()) {
            changeToLandscape();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            changeToLandscape();

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            changeToPortrait();
        }
    }

    public void changeToLandscape() {
//IT GIVES NULLPOINTEREXCEPTION HERE.
        plot.setLayoutParams(new LinearLayout.LayoutParams(500, 300));
    }

    public void changeToPortrait() {
//IT GIVES NULLPOINTEREXCEPTION HERE.
        plot.setLayoutParams(new LinearLayout.LayoutParams(300, 500));
    }

companygraph.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/linearLayout">"

<com.androidplot.xy.XYPlot
android:id="@+id/companyPlot"
android:layout_width="300px"
android:layout_height="500px"
title=""/>
</LinearLayout>

ANY HELP WILL BE LIFE-SAVER !!!

GAMA
  • 5,958
  • 14
  • 79
  • 126
  • 1
    you dont need to do anything in onConfigChanged() method, you just have to mention in android mainfest.xml, that android:configChanges="orientation" and make another folder in resource layout-land with same name xml file and all the ids even same, android os will automatically change the layout accordingly the landscape or portrait. You dont need to listen the onconfigchanged to just change the layout parameters of any view and wont even need to mention the onConfigChanged() in the program if you day by creating the same xml file with same name in layout-land folder as respect to landscape. – mayank_droid Dec 26 '11 at 13:25
  • I tried what u just stated and it works. But it comes with the **huge** drawback that activity gets re-launched on orientation change :( – GAMA Dec 27 '11 at 04:24
  • no it cant be get restarted, are you still mentioning something in onConfigChanged()?...Remove the method..!! – mayank_droid Dec 27 '11 at 06:09
  • Tried that. Then screen doesn't get re-sized as shown here : http://i.imgur.com/DbIso.png – GAMA Dec 27 '11 at 09:17
  • 1
    did u change the layout according to landscape layout...?...just keep the all name same filename,view's IDs, all of it but just arrange them how u want it in portrait screen and arrange the other landscape file as u want to arrange, simple...!!! i think u just creating the file with same name in layout-land folder..but u have to change it;s layout too as u want to make it look in landscape.., like just changing the width and height in the layout-land folder file – mayank_droid Dec 27 '11 at 14:02
  • please follow this link http://apcmag.com/working-with-layouts-and-orientation-changes.htm...it will give you understanding of screen orientation properly.. – mayank_droid Dec 27 '11 at 14:07
  • I've adjusted the layout as required for landscape in layout-land folder. Check this [link](http://pastebin.com/M9xfEccB) – GAMA Dec 28 '11 at 04:24
  • nops :(... It gives the output as shown in above link... – GAMA Dec 28 '11 at 13:25
  • did u follow the link i mentioned i posted here?...this is all i can,i cant say anything further than that..untill i see the code by running it my self – mayank_droid Dec 28 '11 at 13:56
  • yeah, I done it. Final words , I can *either* use (1.) `android:configChanges="orientation"` hence not able to adjust the layout for different orientation *AUTOMATICALLY* ***OR*** (2.) `use *layout* and *layout-land* concept Hence not able to *SAVE ACTIVITY STATE*`.... ***NOT BOTH*** .... **SAVING ACTIVITY STATE** is more important for me, so I chose first approach. – GAMA Dec 30 '11 at 05:21

1 Answers1

1

Looks like you plot object is null, try reinitializing it from findViewById(..)

Rajdeep Dua
  • 11,190
  • 2
  • 32
  • 22
  • that worked. But the mistake I was doing is to call `changeToLandscape()` before `plot = (XYPlot) findViewById(R.id.companyPlot);`. Anyways, thnx. – GAMA Dec 26 '11 at 12:02