Android进度条为空

时间:2014-02-10 12:12:19

标签: android android-fragments progress-bar

我有一个奇怪的问题。我的布局文件中有 ProgressBar onPreExecute 我显示进度条, onPostExecute 我隐藏了进度条。但是在 onPreExecute 方法ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar2);中,progressBar始终为 null ,其中同一行代码在 onPostExecute

MainActivity中的异步任务

@Override
        protected void onPreExecute() {
            super.onPreExecute();
            ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar2);
            progressBar.setVisibility(View.VISIBLE);
        }


@Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar2);
            progressBar.setVisibility(View.GONE);

            mainList.setAdapter(sectionAdapter);
            sectionAdapter.notifyDataSetChanged();
            mainList.setOnItemClickListener(new mainListClicked());

        }

* progressBar位于fragment_mainview.xml *

<?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>

有关详细信息,我使用片段填充 activity_main.xml 布局 fragment_main.xml ,如下所示。

@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();
    }

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>

0 个答案:

没有答案