设置视图对象参数和QUOT;高度和宽度QUOT;应用程序崩溃使用空引用错误 [英] Setting view object parameters "height and width" crashes app with null reference error
问题描述
我想一个新的视图添加到我的线性布局 activity_main.xml中
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingLeft =0dp
机器人:paddingRight =0dp
机器人:paddingTop =0dp
机器人:paddingBottom会=0dp
工具:上下文=。MainActivity
机器人:ID =@ + ID / activity_main
机器人:方向=垂直> < ListView控件
机器人:ID =@ + ID / ListView1的
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_alignParentBottom =真
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真/>< / LinearLayout中>
使用这个code
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); //创建并添加新的视图activity_main
的LinearLayout activityMain =(的LinearLayout)findViewById(R.id.activity_main);
查看playControlsPanelMinimized =新的视图(本);
activityMain.addView(playControlsPanelMinimized);
然而,当我尝试设置高度和宽度
//设置宽度和高度code
ViewGroup.LayoutParams PARAMS = playControlsPanelMinimized.getLayoutParams();
params.height = 10;
params.width = 10;
通过产生的code
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); //创建并添加新的视图activity_main
的LinearLayout activityMain =(的LinearLayout)findViewById(R.id.activity_main);
查看playControlsPanelMinimized =新的视图(本);
ViewGroup.LayoutParams PARAMS = playControlsPanelMinimized.getLayoutParams(); //设置宽度和高度code
ViewGroup.LayoutParams PARAMS = playControlsPanelMinimized.getLayoutParams();
params.height = 10;
params.width = 10; activityMain.addView(playControlsPanelMinimized);
在此应用崩溃启动,并在日志猫以下错误
了java.lang.RuntimeException:无法启动活动...
显示java.lang.NullPointerException:尝试写入到外地'廉政
在一个空对象引用android.view.ViewGroup $ LayoutParams.height
在换句话说, PARAMS
对象为空,我不能写入。我认为这是改变视图对象的高度和宽度的正确方法。
修改
这是个人建议我改变相应的高度和宽度
playControlsPanelMinimized.setLayoutParams(新ViewGroup.LayoutParams(100,100));
虽然应用程序不再崩溃启动时用这种方法,我没有看到新的视图。
修改
我添加了一个背景颜色,但仍我没有看到视图
playControlsPanelMinimized.setBackgroundColor(Color.parseColor(#00FFFF));
您可以试试,
playControlsPanelMinimized.setLayoutParams(新ViewGroup.LayoutParams(10,10));
而不是
ViewGroup.LayoutParams PARAMS = playControlsPanelMinimized.getLayoutParams(); //设置宽度和高度code
ViewGroup.LayoutParams PARAMS = playControlsPanelMinimized.getLayoutParams();
params.height = 10;
params.width = 10;
您可以aother线性布局,并注入新的UI元素到而不是父母的LinearLayout。
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingLeft =0dp
机器人:paddingRight =0dp
机器人:paddingTop =0dp
机器人:paddingBottom会=0dp
工具:上下文=。MainActivity
机器人:ID =@ + ID / activity_main
机器人:方向=垂直> <! - 这里有另一个线性布局 - > < ListView控件
机器人:ID =@ + ID / ListView1的
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_alignParentBottom =真
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真/>< / LinearLayout中>
I am trying to add a new view to my linear layout activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context=".MainActivity"
android:id="@+id/activity_main"
android:orientation="vertical">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</LinearLayout>
Using this code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create and add new view to activity_main
LinearLayout activityMain = (LinearLayout)findViewById(R.id.activity_main);
View playControlsPanelMinimized = new View(this);
activityMain.addView(playControlsPanelMinimized);
However, When I try to set the height and width
// Set height and width code
ViewGroup.LayoutParams params = playControlsPanelMinimized.getLayoutParams();
params.height = 10;
params.width = 10;
With the resulting code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create and add new view to activity_main
LinearLayout activityMain = (LinearLayout)findViewById(R.id.activity_main);
View playControlsPanelMinimized = new View(this);
ViewGroup.LayoutParams params = playControlsPanelMinimized.getLayoutParams();
// Set height and width code
ViewGroup.LayoutParams params = playControlsPanelMinimized.getLayoutParams();
params.height = 10;
params.width = 10;
activityMain.addView(playControlsPanelMinimized);
The app crashes on start up, with the following error in the log cat
java.lang.RuntimeException: Unable to start activity ...
java.lang.NullPointerException: Attempt to write to field 'int
android.view.ViewGroup$LayoutParams.height' on a null object reference
In other words, the params
object is null, and I cannot write to it. I thought this was the correct way to change height and width of a view object.
edit
An individual suggested I change height and width accordingly
playControlsPanelMinimized.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
Although the app no longer crashes on startup with this method, I do not see the new view.
edit
I have added a background color but still I see no view
playControlsPanelMinimized.setBackgroundColor(Color.parseColor("#00FFFF"));
You can try,
playControlsPanelMinimized.setLayoutParams(new ViewGroup.LayoutParams(10, 10));
instead of
ViewGroup.LayoutParams params = playControlsPanelMinimized.getLayoutParams();
// Set height and width code
ViewGroup.LayoutParams params = playControlsPanelMinimized.getLayoutParams();
params.height = 10;
params.width = 10;
You can have aother linear layout and inject your new UI element to that rather than the parent LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context=".MainActivity"
android:id="@+id/activity_main"
android:orientation="vertical">
<!-- Have another linear layout here-->
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</LinearLayout>
这篇关于设置视图对象参数和QUOT;高度和宽度QUOT;应用程序崩溃使用空引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!