设置视图对象参数"高度和宽度"崩溃应用程序与空引用错误

时间:2015-10-06 23:24:02

标签: android

我正在尝试为线性布局添加新视图<xsl:template match="@*"> <xsl:attribute name="data-{local-name()}"> <xsl:call-template name="replace-html-codes"> <xsl:with-param name="text" select="."/> </xsl:call-template> </xsl:attribute> </xsl:template> <!-- copy all nodes --> <xsl:template match="node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>

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>

但是,当我尝试设置高度和宽度时

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

使用生成的代码

    // Set height and width code
    ViewGroup.LayoutParams params = playControlsPanelMinimized.getLayoutParams();
    params.height = 10;
    params.width = 10;

应用程序在启动时崩溃,日志cat中出现以下错误

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

换句话说, 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 对象为空,我无法写入它。我认为这是改变视图对象高度和宽度的正确方法。

修改

个人建议我相应地改变高度和宽度

params

虽然使用此方法启动时应用程序不再崩溃,但我看不到新视图。

修改

我添加了背景颜色,但仍然看不到视图

playControlsPanelMinimized.setLayoutParams(new ViewGroup.LayoutParams(100, 100)); 

2 个答案:

答案 0 :(得分:2)

你可以尝试,

logging.basicConfig

而不是

playControlsPanelMinimized.setLayoutParams(new ViewGroup.LayoutParams(10, 10));

您可以使用另一个线性布局,并将新的UI元素注入其中而不是父线性层。

        ViewGroup.LayoutParams params = playControlsPanelMinimized.getLayoutParams();

        // Set height and width code
        ViewGroup.LayoutParams params = playControlsPanelMinimized.getLayoutParams();
        params.height = 10;
        params.width = 10;

答案 1 :(得分:0)

尝试这样的事情:

ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, height);
playControlsPanelMinimized.setLayoutParams(params);