0

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

asked Oct 17, 2019 at 3:32

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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);
answered Oct 17, 2019 at 4:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.