FindViewbyId始终在Fragment中返回Null

时间:2014-12-05 12:38:04

标签: android android-activity android-fragments findviewbyid

我有一个xml文件,如下所示:

One.xml

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@drawable/additemsbackground"
        android:orientation="horizontal"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp">
  <ImageView
            android:id="@+id/btnaddtxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical"
            android:layout_marginRight="10dp"
            android:adjustViewBounds="true"
            android:src="@drawable/add" />
    </LinearLayout>

这是我的MainFragment:

  @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
       LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
       View myView = layoutInflater.inflate(R.layout.One,
            null);
       ImageView btnadd=(ImageView)myView.findViewById(R.id.btnaddtxt);
       btnadd.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                    Toast.makeText(getApplicationContext(),
                              "Clicked Add Text Button" , Toast.LENGTH_LONG)
                              .show();
                }
            });
  }

当我尝试获取我ImageView中显示的MainFragment时,它始终返回null。

有谁能说我这样做的正确方法是什么?

3 个答案:

答案 0 :(得分:5)

使用片段时,在onCreateView(...)中初始化视图,而不是在onCreate(...)中。像这样的东西

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.delivery_boys, container,
                false);
        ImageView btnadd=(ImageView)rootView .findViewById(R.id.btnaddtxt);

        return rootView;
    }

答案 1 :(得分:0)

你想在片段类

中的onCreateView中膨胀视图
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view= inflater.inflate(R.layout.delivery_boys, container,
                false);
        ImageView img=(ImageView)view.findViewById(R.id.your_img_view);

        return view;
    }

onCreateView函数附加到活动中,您可以在onCreate中获得查看通胀机会。方法片段已初始化且尚未附加到活动。

答案 2 :(得分:0)

@Override
public View setContentView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (mChangeVehiclesView == null) {
        mChangeVehiclesView = inflater.inflate(
                R.layout.frag_vehicles_registered, null);
    } else {
        ((ViewGroup) mChangeVehiclesView.getParent())
                .removeView(mChangeVehiclesView);
    }
mvehicleListView = (ListView) mChangeVehiclesView
            .findViewById(R.id.frag_vehicle_registeres_list);
    return mChangeVehiclesView;
}