setLayoutParams 上出现 nullPointerException

发布于 2024-12-23 01:15:39 字数 1747 浏览 2 评论 0原文

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

但是,如果我将其启动为横向模式,那么当我尝试使用 setLayoutParams 调整视图大小时,它会给出 nullPointerException

一段代码

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

任何帮助都会救命!!!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-12-30 01:15:39

看起来您的绘图对象为空,请尝试从 findViewById(..) 重新初始化它

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文