TransWikia.com

android studio : My app crashes immediately after start

Stack Overflow Asked by user13930404 on November 7, 2021

My problem is fixed when I declare the img1 and img2 variables in the onCreate(). However, I really need those variables to be declared globally. Any ideas?

package com.example.ui;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;




public class MainActivity extends AppCompatActivity implements View.OnClickListener  {
    /*img1 and img2 veriables below*/
    ImageView img1 = findViewById(R.id.img1);
    ImageView img2 = findViewById(R.id.img2);
    @Override

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

        /*my problem is fixed when I declare those variables here, but I need them to be global*/
        img1.setOnClickListener(this);
        img2.setOnClickListener(this);

          }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        switch(id){
            case R.id.img1:
                Toast.makeText(this, "img1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.img2:
                Toast.makeText(this, "img2", Toast.LENGTH_SHORT).show();

        }
    }
}

5 Answers

I can't really understand what you means.Is that you want to img1 and img2 becoming globally in the activity or in the app?

Actually I guess you misunderstand the sequence of the program. After onCreate() method, the phone window will be initialized. Then setContentView() method bind the view in the phone window. So you should bind imageView in onCreate() method after setContentView().

public class MainActivity extends AppCompatActivity implements View.OnClickListener  {
    /*img1 and img2 veriables below*/
    ImageView img1;
    ImageView img2;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img1 = findViewById(R.id.img1);
        img2 = findViewById(R.id.img2);
        /*my problem is fixed when I declare those variables here, but I need them to be global*/
        img1.setOnClickListener(this);
        img2.setOnClickListener(this);

          }
}

Answered by yanyusanqian on November 7, 2021

You cannot use findViewById there, try this:

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener  {
private ImageView img1;
private ImageView img2;
@Override

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

    img1 = findViewById(R.id.img1);
    img2 = findViewById(R.id.img2);
    
    img1.setOnClickListener(this);
    img2.setOnClickListener(this);

      }

@Override
public void onClick(View view) {
    int id = view.getId();

    switch(id){
        case R.id.img1:
            Toast.makeText(this, "img1", Toast.LENGTH_SHORT).show();
            break;
        case R.id.img2:
            Toast.makeText(this, "img2", Toast.LENGTH_SHORT).show();

    }
}

Here is the documentation

Answered by Azhagthott on November 7, 2021

Modify your

 ImageView img1;//only declaration
 ImageView img2;
  @Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
       ImageView img1 = findViewById(R.id.img1);
      ImageView img2 = findViewById(R.id.img2);
    /*my problem is fixed when I declare those variables here, but I need them to be global*/
    img1.setOnClickListener(this);
    img2.setOnClickListener(this);

      }

Answered by Wini on November 7, 2021

You are finding the view before it is attaching to your class

This is correct way:

public class MainActivity extends AppCompatActivity implements View.OnClickListener  {
    /*img1 and img2 veriables below*/
    ImageView img1;
    ImageView img2;
    @Override

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

     img1 = findViewById(R.id.img1);
     img2 = findViewById(R.id.img2);

        /*my problem is fixed when I declare those variables here, but I need them to be global*/
        img1.setOnClickListener(this);
        img2.setOnClickListener(this);

          }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        switch(id){
            case R.id.img1:
                Toast.makeText(this, "img1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.img2:
                Toast.makeText(this, "img2", Toast.LENGTH_SHORT).show();

        }
    }
}

Answered by chand mohd on November 7, 2021

You should have these lines :

ImageView img1 = findViewById(R.id.img1);

ImageView img2 = findViewById(R.id.img2);

inside the OnCreate method after "setContentView(R.layout.activity_main);"

Answered by Sneha on November 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP