-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() 的空指针异常。

4

1 回答 1

1

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

于 2012-08-16T09:32:01.893 回答