Skip to content Skip to sidebar Skip to footer

Setcontentview And Findviewbyid Error

I am trying to move an application from android from an activity to a fragment mistake and I jump right on the part of setContentView and in the findViewById and no longer do that

Solution 1:

You should retrieve the ID from the contentView by inflating the contentView. When you are working with fragments. You do this as follows:

ViewcontentView= inflater.inflate(R.layout.tactica, container, false);

That way you can find Id's like this:

contentView.findViewById

Solution 2:

Use this code

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {         
          Viewtactics= inflater.inflate(R.layout.tactica, container, false);
           cuadrado = (ImageView)tactics.findViewById(R.id.imageView1);
          cuadrado.setOnTouchListener(handlerMover);
          return tactics;
    }

Here you dont have to use this two lines in onCreateView Method

super.onCreate(savedInstanceState);
setContentView(R.layout.tactica);

And you have to initialize imageview control after inflating the file and not before

Post a Comment for "Setcontentview And Findviewbyid Error"