[英]setOnClickListener button doesn't work Android studio
I'm new in java and I have tried to make a food app in android studio.我是java新手,我试图在android studio中制作一个食物应用程序。 I have a bar in app and when I click on cart my app is crashing.
我在应用程序中有一个栏,当我点击购物车时,我的应用程序崩溃了。
Here is my MainActivity这是我的 MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView.Adapter adapter, adapter2;
private RecyclerView recyclerViewCategotyList, recyclerViewPopularList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerViewCategoty();
recyclerViewPopular();
bottomNavigation();
}
private void bottomNavigation() {
LinearLayout homeBtn=findViewById(R.id.homeBtn);
LinearLayout cartBtn=findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,CartActivity.class));
}
});
}
And I have this error: at com.example.food.Activity.MainActivity$2.onClick(MainActivity.java:48)我有这个错误:在 com.example.food.Activity.MainActivity$2.onClick(MainActivity.java:48)
This is CartActivity这是购物车活动
public class CartActivity extends AppCompatActivity {
private RecyclerView.Adapter adapter;
private RecyclerView recyclerViewList;
private ManagementCart managementCart;
private TextView totalFeeTxt, taxTxt, deliveryTxt, totalTxt, emptyTxt;
private double tax;
private ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
managementCart = new ManagementCart(this);
initView();
initList();
bottomNavigation();
calculateCard();
}
private void bottomNavigation() {
LinearLayout homeBtn = findViewById(R.id.homeBtn);
LinearLayout cartBtn = findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
}
I don't know what's wrong and why my setOnClickListener doesn't work.我不知道出了什么问题以及为什么我的 setOnClickListener 不起作用。
Thank you.谢谢你。
I think your button R.id.homeBtn is not LinearLayout, can you show your xml file?我认为您的按钮 R.id.homeBtn 不是 LinearLayout,您可以显示您的 xml 文件吗?
If your buttons are Button classes, try this如果你的按钮是按钮类,试试这个
private void bottomNavigation() {
Button homeBtn = findViewById(R.id.homeBtn);
Button cartBtn = findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.