Android进度条为空

Android进度条为空,android,android-fragments,progress-bar,Android,Android Fragments,Progress Bar,我有点奇怪的问题。我的布局文件中有一个进度条。onPreExecute显示进度条,onPostExecute隐藏进度条。但在onPreExecute方法ProgressBar ProgressBar=ProgressBar findViewByIdR.id.progressBar2中;progressBar始终为空,其中同一行代码在onPostExecute中可以正常工作 MainActivity中的异步任务 *progressBar位于fragment_mainview.xml中* 而acti

我有点奇怪的问题。我的布局文件中有一个进度条。onPreExecute显示进度条,onPostExecute隐藏进度条。但在onPreExecute方法ProgressBar ProgressBar=ProgressBar findViewByIdR.id.progressBar2中;progressBar始终为空,其中同一行代码在onPostExecute中可以正常工作

MainActivity中的异步任务

*progressBar位于fragment_mainview.xml中*

而activity_main.xml如下所示

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

     <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >



        <ListView 
            android:id="@+id/mainListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </FrameLayout>

    <ListView android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:dividerHeight="0dp"
            android:background="#FFF"/>
</android.support.v4.widget.DrawerLayout>

何时何地调用AsycTask.execute?您好@拉杰什普。我已经编辑了这个问题。首先,我用片段填充主视图。在此之后,我将执行后台任务,完成此任务需要5-7秒;你在你的碎片里写了这段代码,然后我的解决方案不正确,对不起。是的,我试图从和进程以及线程中了解更多:LoadListInBackground是MainActivity的内部类,我在一个单独的类中扩展片段
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >    
     <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />  
</RelativeLayout>
@Override


    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

        FragmentManager fragmentManager = getSupportFragmentManager();
                  FragmentTransaction fragmentTransaction = 
                  fragmentManager.beginTransaction();
                  MainViewFragment mianFragment = new MainViewFragment();
                  fragmentTransaction.replace(R.id.frame_container, mianFragment);
                  fragmentTransaction.commit();

            mDrawerList = (ListView) findViewById(R.id.left_drawer);
            mainList = (ListView) findViewById(R.id.mainListView);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

              new LoadListInBackground().execute();
    }
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

     <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >



        <ListView 
            android:id="@+id/mainListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </FrameLayout>

    <ListView android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:dividerHeight="0dp"
            android:background="#FFF"/>
</android.support.v4.widget.DrawerLayout>