Search code examples
androidfindviewbyid

how use "findViewById" in android studio 2.3.1?


i use android studio 2.3.1 , and don't have "findViewById" when use button and setOnClickListener for button !

and use this :

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        findViewById(R.id.button2);
        BTN_PROGRESS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doWork();
            }
        });
    }

    @Override
    public View findViewById(@IdRes int id) {
        BTN_PROGRESS = (Button) findViewById(R.id.button2);
        PRG_MAIN = (ProgressBar) findViewById(R.id.progressBar);
        return super.findViewById(id);
    }

    private void doWork() {
        // my code
    }
}

when run app not error happened ! but app be force close ! how handler this problem ?


Solution

  • findViewById is already defined on the Activity class, and it works as intended. You don't need to override it.

    Your app crashes with a Stackoverflow because you're doing infinite recursion

    So, maybe call your method private void findViews(), then do this

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        findViews();
        BTN_PROGRESS.setOnClickListener