Case:
so you create your awesome UI in xml like and then in activity file you code that should change your progress bar and your program crashed and IDE said that
Error message looks like:
findViewById(R.id.cozyUIElement) is null
What went wrong ?
You need set content view before you can match them with your class from view
Right way to do it is:
setContentView(R.layout.cozy); <--- THIS MUST BE FIRST
cozyProgressBar
= (ProgressBar) findViewById(R.id.cozyUIElement
); <--THIS method need appear after setContentView(...)!
So check your onCreate() method and look are you using any
findViewById() method before
setContentView(R.layout.cozy) if you found any,then you found your problem :)
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/cozyUIElement"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:max="100"
android:visibility="visible" />
</LinearLayout>
and then in your activity file
package ,imports and other spam
(...)
public class CozyActivity extends Activity{
ProgressBar cozyProgressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cozy);
cozyProgressBar
= (ProgressBar) findViewById(R.id.cozyUIElement
);
cozyProgressBar.setProgress(20);
}
No comments:
Post a Comment