1

I have a Tab View with three layouts. Each layout is divided into 4 parts using Frame layout and each one has a button. So there are total of 12 buttons. Since the View is First Layout, buttons in other two layouts return a null pointer exception. I tried include in XML but the buttons get merged and the app runs. Any Solution. Working Day And Night.

View rootView = inflater.inflate(R.layout.fragment_main,container,false);
View rootView = inflater.inflate(R.layout.fragment_2,container,false);
View rootView = inflater.inflate(R.layout.fragment_3,container,false);

b1=(Button)rootView.findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View a){...}
});

The Buttons in Fragment_main are Running but buttons in fragment_2 & fragment_3 return Null Pointer Exception.

Note : I used Eclipse's Tab Layout.

Thank You For Help in advance

2 Answers2

0

You inflate rootView 3 times, which last one is inflated. First two layouts and its childs are not accessable.

Jemshit
  • 9,501
  • 5
  • 69
  • 106
0

change the code as below, it works fine according to my knowledge.

    View rootView = inflater.inflate(R.layout.fragment_main,container,false);
    View rootView1 = inflater.inflate(R.layout.fragment_2,container,false);
    View rootView2 = inflater.inflate(R.layout.fragment_3,container,false);

    b1=(Button)rootView.findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener(){
       @Override
       public void onClick(View a){...}
    });

    b2=(Button)rootView1.findViewById(R.id.button2);
    b2.setOnClickListener(new View.OnClickListener(){
       @Override
       public void onClick(View a){...}
    });

It works fine for you, if you have any queries, do let me know.

Parth Bhayani
  • 1,894
  • 3
  • 17
  • 35