Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(ProgressBar) findViewById(R.id.ProgressBar) returns null

progress bar always return null

 public void calcola(View view) {
     final Dialog myDialog = new Dialog(this);
        myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        myDialog.setContentView(R.layout.calcdialog);
        myDialog.setCancelable(false);

        Typeface typeFace = Typeface.createFromAsset(getAssets(),"font/Flower.ttf");
        mProgress = (ProgressBar) findViewById(R.id.ProgressBar);

        TextView calc = (TextView) myDialog.findViewById(R.id.textView1);
        calc.setTypeface(typeFace);
        myDialog.show(); }

this is my xml

 <ProgressBar
    android:id="@+id/ProgressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:maxHeight="30dp"
    android:minHeight="30dp" />

the textview in the same layout works perfectly, any idea?

thanks

like image 502
Vulneraria Avatar asked Sep 06 '13 13:09

Vulneraria


People also ask

Why is findViewById returning NULL?

FindViewById can be null if you call the wrong super constructor in a custom view. The ID tag is part of attrs, so if you ignore attrs, you delete the ID.

What does findViewById return?

findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .

What is indeterminate Progress bar android?

ProgressBar is used to display the progress of an activity while the user is waiting. You can display an indeterminate progress (spinning wheel) or result-based progress.


1 Answers

(ProgressBar) findViewById(R.id.ProgressBar) returns null

Because You are using findViewById(R.id.ProgressBar); instead of myDialog.findViewById(R.id.ProgressBar);

So your Code Shoud be

 mProgress = (ProgressBar) myDialog.findViewById(R.id.ProgressBar);
like image 57
Tarsem Singh Avatar answered Nov 10 '22 06:11

Tarsem Singh