The app crashes automatically here but shows no error
public class MainActivity extends AppCompatActivity {
Button b1, b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1.findViewById(R.id.btn1);
b2.findViewById(R.id.btn2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast1 = Toast.makeText(getApplicationContext(), "I am short", Toast.LENGTH_SHORT);
toast1.show();
Intent intent1 = new Intent(Intent.ACTION_VIEW);
intent1.setData(Uri.parse("http://www.facebook.com"));
startActivity(intent1);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Open();
Toast toast2 = Toast.makeText(getApplicationContext(), "I am here why to fear", Toast.LENGTH_LONG);
toast2.show();
}
});
}
public void Open() {
Intent intent2 = new Intent(this, Main2.class);
startActivity(intent2);
}
}
italic I can't find the error on this code but the application file is not opening. it says the application has stopped
2 Answers 2
Change this Code
b1.findViewById(R.id.btn1);
b2.findViewById(R.id.btn2)
With
b1 = findViewById(R.id.btn1);
b2 = findViewById(R.id.btn2);
answered Oct 17, 2019 at 3:49
Muhammad Usman Butt
5434 silver badges15 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Change this Code
b1.findViewById(R.id.btn1);
b2.findViewById(R.id.btn2);
With
b1 = (Button)findViewById(R.id.btn1);
b2 = (Button)findViewById(R.id.btn2);
Comments
default