继承fragment的类中,初始化控件问题

这是类的代码片段

 public class CenterFragment extends Fragment implements OnClickListener{

    private ListView lv_order;//订单列表
    private List<Map<String, Object>> orderMessage;//订单列表数据源
    private SimpleAdapter myAdapter;
    private TextView title_name, title_ico;//订单头布局
    private ImageView iv_dish;//订单内容布局
    private RelativeLayout order_title;
    private LinearLayout order_detail;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.centerfragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();//初始化控件
        initOrder();//数据源
        myAdapter = new SimpleAdapter(this.getActivity(), orderMessage,
                R.layout.oder_item, new String[] { "name", "icon", "price" },
                new int[] { R.id.title_name, R.id.iv_dish, R.id.order_price });
        lv_order.setAdapter(myAdapter);
    }

    /*
     * 初始化控件
     */
    private void initView() {
        lv_order = (ListView) getActivity().findViewById(R.id.lv_order);
        System.out.println(lv_order);
        System.out.println("===================");
        title_name = (TextView) getActivity().findViewById(R.id.title_name);
        System.out.println(title_name);
        title_ico = (TextView) getActivity().findViewById(R.id.title_ico);
        iv_dish = (ImageView) getActivity().findViewById(R.id.iv_dish);

        //设置监听
        title_name.setOnClickListener(this);
        title_ico.setOnClickListener(this);
        iv_dish.setOnClickListener(this);
    }

这是布局代码

 <?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="120dp"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/order_title"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:padding="5dp" 
        android:gravity="center">

        <TextView 
            android:id="@+id/title_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:textSize="14sp"
            android:textColor="@color/text_color"
            android:text="肯打鸡"/>
        <TextView 
            android:id="@+id/title_ico"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/title_name"
            android:layout_marginLeft="20dp"
            android:background="@drawable/paycommon__ic_global_arrow_right"/>

        <TextView
            android:id="@+id/order_state"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:textSize="12sp"
            android:textColor="@color/text_color"
            android:text="订单正在处理" />
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_alignParentBottom="true"
            android:layout_alignBottom="@id/title_name"
            android:background="#22000000"/>
    </RelativeLayout>


    <LinearLayout 
        android:id="@+id/order_detail"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:padding="5dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView 
            android:id="@+id/iv_dish"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/takeout_ic_order_empty"/>

        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="vertical">

            <TextView 
                android:id="@+id/order_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ff0000"
                android:text="$9.8"/>

            <TextView 
                android:id="@+id/order_take_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10sp"
                android:textColor="@color/text_color"
                android:text="09-11 11:36"/>

            <TextView 
                android:id="@+id/order_synopsis"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="10sp"
                android:textColor="@color/text_color"
                android:text="由肯打鸡提供外卖和配送服务"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

    lv_order = (ListView) getActivity().findViewById(R.id.lv_order);
    System.out.println(lv_order);
    title_name = (TextView) getActivity().findViewById(R.id.title_name);
    System.out.println(title_name);
    title_ico = (TextView) getActivity().findViewById(R.id.title_ico);
    iv_dish = (ImageView) getActivity().findViewById(R.id.iv_dish);

lv_order可以初始化,后面的打印为空,设置不了监听
请问这是怎么回事??

查看全部
a1175140101
何必丶何苦呢
2015/09/08 01:23
  • fragment
  • 控件
  • relativelayout
  • 布局
  • class
  • 点赞
  • 收藏
  • 回答
    私信

2个回复