-1

私は Android アプリケーションで作業しています。私のアプリでは、テキストビューの配置を動的に変更しました。テキストビューは相対レイアウトです。以下は私のサンプル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 例外が発生しています。

4

1 に答える 1

1

nullpointer 例外の理由は次のとおりです: R.id.snd_txtを見つけようとしていますが、レイアウトにはsnt_txtがあるため、レイアウトに存在しないため、 snd_txt id のビューを見つけることはできません。

于 2012-08-16T09:32:01.893 に答える