为啥这地方总是报错啊 求大神指点!
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button1,button2,button3,button4;
private LinearLayout linearLayout;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment4 fragment4;
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button1=findViewById(R.id.bt1);
button2=findViewById(R.id.bt2);
button3=findViewById(R.id.bt3);
button4=findViewById(R.id.bt4);
fragment1=new Fragment1();
fragment2=new Fragment2();
fragment3=new Fragment3();
fragment4=new Fragment4();
fragmentManager=getSupportFragmentManager();
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
// button1.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Fragment1 fragment1=new Fragment1();
// FragmentManager fragmentManager=getSupportFragmentManager();
// FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
// fragmentTransaction.replace(R.id.ll,fragment1);
// fragmentTransaction.commit();
// }
// });
// button2.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Fragment1 fragment1=new Fragment1();
// FragmentManager fragmentManager=getSupportFragmentManager();
// FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
// fragmentTransaction.replace(R.id.ll,fragment1);
// fragmentTransaction.commit();
// }
// });
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.bt1:
clickBt1();
break;
case R.id.bt2:
clickBt2();
break;
case R.id.bt3:
clickBt3();
break;
case R.id.bt4:
clickBt4();
break;
}
}
public void clickBt1()
{
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.ll,fragment1);
fragmentTransaction.commit();
button1.setTextColor(Color.RED);
button2.setTextColor(Color.BLACK);
button3.setTextColor(Color.BLACK);
button4.setTextColor(Color.BLACK);
}
public void clickBt2()
{
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.ll,fragment2);
fragmentTransaction.commit();
button2.setTextColor(Color.BLUE);
button1.setTextColor(Color.BLACK);
button3.setTextColor(Color.BLACK);
button4.setTextColor(Color.BLACK);
}
public void clickBt3()
{
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.ll,fragment3);
fragmentTransaction.commit();
button3.setTextColor(Color.RED);
button2.setTextColor(Color.BLACK);
button1.setTextColor(Color.BLACK);
button4.setTextColor(Color.BLACK);
}
public void clickBt4()
{
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.ll,fragment4);
fragmentTransaction.commit();
button4.setTextColor(Color.RED);
button2.setTextColor(Color.BLACK);
button3.setTextColor(Color.BLACK);
button1.setTextColor(Color.BLACK);
}
}
onCreate里面缺少setContentView。
你的布局呢? 不添加布局进去,怎么识别控件id.
没有setContentView方法 你怎么findviewbyId =_=