设置视图的左边距... NullPointerException 来了

set left margin for view ... NullPointerException is coming

我的代码这是一个适配器,我想在列表视图中将我的布局动态地左右对齐...空指针异常出现在 relativeParams.leftMargin=80;

Java:

public View getView(int position, View view, ViewGroup parent)
    {
        ViewHolder viewHolder;
        ChatItem chatItem = getItem(position);
        viewAligned = view;
        if(view == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater)  
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.cell_messages_layout, null);
            viewHolder = new ViewHolder();
            viewHolder.message = (TextView) view.findViewById(R.id.fromZoneMessage1);
            viewHolder.rl = (RelativeLayout) view.findViewById(R.id.messageLayout);
            view.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.message.setText(chatItem.getSenderMessage());
        if (chatItem.getAllignment() == 0)
        {
//          viewHolder.message.setGravity(Gravity.LEFT);
            viewHolder.message.setBackgroundResource(R.drawable.sent_message);
//          view.setBackgroundResource(R.drawable.sent_message);
        }
        else {
//          viewHolder.rl.setBackgroundResource(R.drawable.recieved_message);
//          view.holder.rl.set
//          viewHolder.message.setGravity(Gravity.RIGHT);
            viewHolder.message.setBackgroundResource(R.drawable.recieved_message);

            RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams) viewHolder.rl.getLayoutParams();
            relativeParams.leftMargin=80;
            viewHolder.rl.setLayoutParams(relativeParams);
//          LayoutParams lp = (LayoutParams) view.getLayoutParams();
        }
        return view;
    }

我的布局是....

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/messageLayout" 
   >


    <TextView 
        android:id="@+id/fromZoneMessage1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="10dp"
        android:padding="5dp"
        android:text="Hey Jack"
        />

</RelativeLayout>

请有人帮我为我的视图留出左边距....

不要使用 RelativeLayout.LayoutParams,而是尝试使用 AbsListView.LayoutParams,因为您的视图在 ListView 中。

我只是将视图类型转换为相对布局并设置其重力...为了设置参数,意味着边距我在主相对布局中采用了另一个虚拟相对布局并进行了调整。

你可以看下面的代码和相对布局

public View getView(int position, View view, ViewGroup parent)
    {
        ViewHolder viewHolder;
        ChatItem chatItem = getItem(position);
        viewAligned = view;
        if(view == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.cell_messages_layout, null);
            viewHolder = new ViewHolder();
            viewHolder.message = (TextView) view.findViewById(R.id.fromZoneMessage1);
            viewHolder.rl = (RelativeLayout) view.findViewById(R.id.messageLayout);
            view.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.message.setText(chatItem.getSenderMessage());
        if (chatItem.getAllignment() == 0)
        {

            viewHolder.message.setGravity(Gravity.LEFT);
            viewHolder.message.setBackgroundResource(R.drawable.recieved_message);
            ((RelativeLayout)view).setGravity(Gravity.RIGHT);
            android.widget.RelativeLayout.LayoutParams params = new LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.leftMargin = 50;
            viewHolder.rl.setLayoutParams(params);

                    }
        else {
            viewHolder.message.setGravity(Gravity.LEFT);
            viewHolder.message.setBackgroundResource(R.drawable.sent_message);
            ((RelativeLayout)view).setGravity(Gravity.LEFT);
            android.widget.RelativeLayout.LayoutParams params = new LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.rightMargin = 50;
            viewHolder.rl.setLayoutParams(params);
        }

        return view;
    }

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/messageLayout" 
       >


    <TextView 
        android:id="@+id/fromZoneMessage1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="10dp"
        android:padding="5dp"
        android:text="Hey Jack"/>

</RelativeLayout>
</RelativeLayout>