具有id引用的TextView对象即将为null

时间:2015-04-02 11:52:35

标签: android

我正在尝试在textView上设置文本,但是对象本身也是空的,即使我已经在xml中定义了id属性。我正在设置要设置的值textViewtextView对象本身为空。

活动:

public class ItemActivity extends Activity {
    ListView lv;
    Context context;
    ArrayList prgmName;
    String dateStr=null;
       @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        final boolean customTitleSupported =
                requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.itemlist);
       if(customTitleSupported){
              getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.item);
       }
        final TextView myTitleText = (TextView) findViewById(R.id.showRoomTextView);
        final TextView textViewStyle = (TextView) findViewById(R.id.textViewStyle);

        ArrayList<InvoiceData> invoiceList = (ArrayList<InvoiceData>) getIntent().getSerializableExtra("invoiceList");
        for(int i=0;i<invoiceList.size();i++){
            if (myTitleText != null) {
                myTitleText.setText(invoiceList.get(i).getCompl_Name());
            }

            String prdNo=invoiceList.get(i).getPrd_No();
            if(prdNo!=null) {
                System.out.println("no..." + invoiceList.get(i).getPrd_No()+""+textViewStyle);
                textViewStyle.setText(invoiceList.get(i).getPrd_No());
            }

        }
        context=this;
        lv=(ListView) findViewById(R.id.listView);
        lv.setAdapter(new CustomAdapter(this,params.....));
    }

,适配器类是:

public class CustomAdapter extends BaseAdapter{
    String [] result;
    String [] result1;
    String [] result2;
    String [] result3;
    String [] result4;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;
    public CustomAdapter(ItemActivity mainActivity, String[] prgmNameList,String[] prgmNameList1,String[] prgmNameList2,String[] prgmNameList3,String[] prgmNameList4) {
        // TODO Auto-generated constructor stub

        context=mainActivity;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stubb
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.tabitem, null);
        rowView.setBackgroundColor(Color.parseColor("#F1F1FF"));
        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });
        return rowView;
    }

}

tabitem xml是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1"
    >
<LinearLayout
    android:layout_width="75dp"
    android:layout_height="wrap_content" android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:textColor="#000000"
        android:text="TextView" />

    <TextView
        android:id="@+id/textViewStyle"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:textColor="#000000"
        />

</LinearLayout>

我试图在活动中设置textViewStyle。

2 个答案:

答案 0 :(得分:2)

如果您想要访问列表中的TextView,则无法在活动中访问它,但您可以在适配器中访问:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
   // ... 
   TextView textviewToModify = rowView.findViewById(R.id.textViewStyle);
   return rowView;
}

您还应该看到ViewHolder模式具有更好的性能。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html

答案 1 :(得分:1)

尝试使用它:

 @Override
            public View getView(int position, View view, ViewGroup parent) {                    
                if (view == null) {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.custom_remarks, null);

                    textViewStyle= (TextView) view.findViewById(R.id.textViewStyle);
                    textViewStyle.setText("TextViewStyle");  
                 }

            return view;
        }