setLayoutParams 上出现 nullPointerException
如果我以纵向模式启动应用程序,它工作正常。
但是,如果我将其启动为横向模式,那么当我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的绘图对象为空,请尝试从 findViewById(..) 重新初始化它
Looks like you plot object is null, try reinitializing it from findViewById(..)