无法在MainActivity(子类)中查看在BaseActivity(父类)中创建的progressBar?

huangapple go评论126阅读模式
英文:

Cannot view progressBar created in BaseActivity(parent) in MainActivity(child)?

问题

以下是翻译好的部分:

我正在尝试为所有重复的操作创建一个单一的基本活动。我尝试将进度条放在BaseActivity中,并尝试在MainActivity中扩展它,但它根本不显示出来。

activity_base.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <FrameLayout
  7. android:id="@+id/activity_content"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. </FrameLayout>
  11. <ProgressBar
  12. android:id="@+id/progressBar"
  13. android:indeterminate="true"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. app:layout_constraintBottom_toBottomOf="parent"
  17. app:layout_constraintLeft_toLeftOf="parent"
  18. app:layout_constraintRight_toRightOf="parent"
  19. app:layout_constraintTop_toTopOf="parent" />
  20. </androidx.constraintlayout.widget.ConstraintLayout>

BaseActivity.java

  1. public abstract class BaseActivity extends AppCompatActivity {
  2. private static final String TAG = "BaseActivity";
  3. public ProgressBar progressBar;
  4. @Override
  5. public void setContentView(int layoutResID) {
  6. ConstraintLayout constraintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
  7. FrameLayout frameLayout = constraintLayout.findViewById(R.id.activity_content);
  8. progressBar = constraintLayout.findViewById(R.id.progressBar);
  9. Log.i(TAG, "setContentView: Progressbar" + progressBar.getVisibility());
  10. getLayoutInflater().inflate(layoutResID, frameLayout, true);
  11. super.setContentView(layoutResID);
  12. }
  13. public void showProgressBar(boolean visibility) {
  14. progressBar.setVisibility(visibility ? View.VISIBLE : View.GONE);
  15. }
  16. }

MainActivity.java

  1. public class MainActivity extends BaseActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. btn_get_data = (Button) findViewById(R.id.btn_get_data);
  7. btn_get_data.setOnClickListener(view -> {
  8. if (progressBar.getVisibility() == View.VISIBLE) {
  9. Log.i(TAG, "onCreate: DISABLING PROGRESS BAR");
  10. showProgressBar(false);
  11. } else {
  12. showProgressBar(true);
  13. }
  14. });
  15. }
  16. }

此外,在应用中还有其他内容,但与进度条无关。是的,我已正确声明了按钮和其他内容。

提前感谢!

英文:

I am trying to make a single base activity for all the redundant actions. I tried putting the progress bar in BaseActivity and tried extending it in MainActivity, But it is not showing up at all.
My Code

activity_base.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;match_parent&quot;&gt;
  6. &lt;FrameLayout
  7. android:id=&quot;@+id/activity_content&quot;
  8. android:layout_width=&quot;match_parent&quot;
  9. android:layout_height=&quot;match_parent&quot;&gt;
  10. &lt;/FrameLayout&gt;
  11. &lt;ProgressBar
  12. android:id=&quot;@+id/progressBar&quot;
  13. android:indeterminate=&quot;true&quot;
  14. android:layout_width=&quot;wrap_content&quot;
  15. android:layout_height=&quot;wrap_content&quot;
  16. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  17. app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
  18. app:layout_constraintRight_toRightOf=&quot;parent&quot;
  19. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  20. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

BaseActivity.java

  1. public abstract class BaseActivity extends AppCompatActivity {
  2. private static final String TAG = &quot;BaseActivity&quot;;
  3. public ProgressBar progressBar;
  4. @Override
  5. public void setContentView(int layoutResID) {
  6. ConstraintLayout constraintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
  7. FrameLayout frameLayout = constraintLayout.findViewById(R.id.activity_content);
  8. progressBar = constraintLayout.findViewById(R.id.progressBar);
  9. Log.i(TAG, &quot;setContentView: Progressbar&quot; + progressBar.getVisibility());
  10. getLayoutInflater().inflate(layoutResID, frameLayout, true);
  11. super.setContentView(layoutResID);
  12. }
  13. public void showProgressBar(boolean visibility) {
  14. progressBar.setVisibility(visibility ? View.VISIBLE : View.GONE);
  15. }
  16. }

MainActivity.java

  1. public class MainActivity extends BaseActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. btn_get_data = (Button) findViewById(R.id.btn_get_data);
  7. btn_get_data.setOnClickListener(view -&gt; {
  8. if (progressBar.getVisibility() == View.VISIBLE) {
  9. Log.i(TAG, &quot;onCreate: DISABLING PROGRESS BAR&quot;);
  10. showProgressBar(false);
  11. } else {
  12. showProgressBar(true);
  13. }
  14. }
  15. }

There are other things in the app, but they have no connection with the progress bar. And yes I have declared the button and other things properly.

Thanks in advance!

答案1

得分: 3

setContentView()方法中的以下代码:

  1. super.setContentView(layoutResID);

更改为:

  1. super.setContentView(constraintLayout);

BaseActivity中进行这个修改。

英文:

Change

  1. super.setContentView(layoutResID);

to

  1. super.setContentView(constraintLayout);

in setContentView() method of the BaseActivity.

huangapple
  • 本文由 发表于 2020年8月4日 15:36:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63242159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定