以编程方式更改相对布局字段的属性

问题描述 投票:-1回答:1

我正在开发一个Android应用程序。在我的应用程序中,我已动态更改了textview对齐方式。文本视图处于相对布局中。以下是我的示例xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/all"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
>
<TextView android:id="@+id/snt_txt"
android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
 android:background="@color/Gold"   
 android:text="df"  
 android:layout_marginLeft="5dp"
 android:layout_marginRight="5dp"
 android:padding="3dp"/>
</RelativeLayout>

并且在Java中,我执行了以下操作

View vi = inflater.inflate(R.layout.smsconversationsent_row, null);
 RelativeLayout nws=(RelativeLayout)vi.findViewById(R.id.all);
 TextView   message=(TextView)vi.findViewById(R.id.snd_txt);
RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)message.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        message.setLayoutParams(params);

但是我收到message.getlayoutparams()的nullpointer异常。

android android-relativelayout
1个回答
1
投票

nullpointer异常的原因如下:您正在尝试查找R.id.snd_txt,但是在布局中您具有snt_txt,因此无法找到具有snd_txt id的视图,因为它没有在布局中不存在。

© www.soinside.com 2019 - 2024. All rights reserved.