setLayoutParams上的nullPointerException

时间:2011-12-26 11:42:50

标签: android nullpointerexception layoutparams

如果我以纵向模式启动应用程序,它可以正常工作。

但如果我将其启动为横向模式,则在尝试使用nullPointerException调整视图大小时会显示setLayoutParams

代码

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

任何帮助都将有益于生命!

1 个答案:

答案 0 :(得分:1)

看起来你的对象是null,尝试从findViewById(..)

重新初始化它