w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Activity Crashing for seemingly no reason ? -Android

Your setContentView should come first before initialization of view.

Replace this

  TextView textView = (TextView) findViewById(R.id.xxtt);


  TextView textView = (TextView) findViewById(R.id.xxtt);

in onCreate

You can findViewById of the current view hierarchy set to the activity. Since you have not set the layout to the activity your findViewById fails. You are probably getting NUllPointerException because of this.


It is better to initialize your views in onCreate since it is called once during the lifecyle of the activity.

Quoting from the docs


onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.