(ProgressBar)findViewById(R.id.ProgressBar)返回null

Vul*_*ria 5 android progress-bar

进度条始终返回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(); }
Run Code Online (Sandbox Code Playgroud)

这是我的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" />
Run Code Online (Sandbox Code Playgroud)

同一布局的textview工作得很完美,任何想法?

谢谢

Tar*_*ngh 5

(ProgressBar) findViewById(R.id.ProgressBar) returns null
Run Code Online (Sandbox Code Playgroud)

因为你使用findViewById(R.id.ProgressBar); 而不是 myDialog.findViewById(R.id.ProgressBar);

所以你的守则应该是

 mProgress = (ProgressBar) myDialog.findViewById(R.id.ProgressBar);
Run Code Online (Sandbox Code Playgroud)