0

I am creating an app where you log in and then access a database of files unique to each user account. I have a continue button linking the "LoginActivity" and "MainActivity" activities together. The code that I have in the LoginActivity file is this:

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.email_create_account_button:
                createAccount(mEdtEmail.getText().toString(), mEdtPassword.getText().toString());
                break;
            case R.id.email_sign_in_button:
                signIn(mEdtEmail.getText().toString(), mEdtPassword.getText().toString());
                break;
            case R.id.sign_out_button:
                signOut();
                break;
            case R.id.continue_button:
                Button btn = (Button)findViewById(R.id.continue_button);
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                break;
        }
    }

and on the MainActivity side I have this:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        upload_button = findViewById(R.id.upload_button);
        showall_button = findViewById(R.id.showall_button);
        progressBar = findViewById(R.id.progressBar);
        imageView = findViewById(R.id.imageView);

        progressBar.setVisibility(View.INVISIBLE);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent();
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent , 2);
            }
        });

What is wrong with my code? I can't seem to figure it out. Please let me know if you need more code from my project. Thanks!

Morgan
  • 17
  • 5
  • 1
    Do you get an Exception thrown? An error message? If so, please include a copy. – Old Dog Programmer Aug 29 '22 at 23:56
  • @James Nope, the app just crashes, no error. – Morgan Aug 30 '22 at 00:06
  • Then how do you know it crashes? – Old Dog Programmer Aug 30 '22 at 00:08
  • 1
    @James Because of the phone emulator in android studio, "com.example.auth keeps stopping" is the emulator message when it crashes. – Morgan Aug 30 '22 at 00:29
  • why your `continue_button` clicked twice? you already inside the method `onClick` and then you trigger again `setOnClickListener` ? – Ticherhaz FreePalestine Aug 30 '22 at 01:33
  • @TicherhazFreePalestine I corrected that, but the button still doesn't work. Thanks for catching that though! – Morgan Aug 30 '22 at 01:48
  • @Morgan Did you fixed the part where you are reassigning onClick after clicking continue button? If yes, then paste your edited code of that part. – Ahsan Ullah Rasel Aug 30 '22 at 02:20
  • 1
    If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question. Please respond using @AlexMamo – Alex Mamo Aug 30 '22 at 05:01
  • When an app crashes, it writes an error message and stack trace to its logcat. Please find those, and add them to your question by clicking the `edit` link under it. Also see https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this and https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Frank van Puffelen Aug 30 '22 at 07:00
  • @AhsanUllahRasel I've edited the code to what it is now. – Morgan Aug 30 '22 at 21:23
  • @AlexMamo java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.auth/com.example.auth.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference – Morgan Aug 30 '22 at 21:31
  • Have you tried AhsanUllahRasel's solution? – Alex Mamo Aug 31 '22 at 07:23

2 Answers2

0

You should initialize your buttons and after that, set onclicklistener to them. For the complete explanation visit Android App Crashes on Button Click

  • They're all initialized and have onclicklistener set. All the other buttons work, just not the continue button. – Morgan Aug 30 '22 at 00:51
0

According to your logcat, it seems that, you are referencing an resource ID which doesn't exists on your XML (activity_main.xml) file.
Check whether the ID imageView exists or not. It might not present in your layout file or it may have different name. Make the ID name same both in activity_main.xml side and MainActivity side.

  • 1
    Thank you! activity_main.xml had "imageView2" from a previous edit and I forgot to change it over. Works now. – Morgan Aug 31 '22 at 19:33