ProgressBar的AsyncTask的onPreExcute中的NullpointerException

时间:2014-03-24 18:18:59

标签: android android-asynctask nullpointerexception android-progressbar

当我尝试将ProgessBar设置为onPreExecute AsyncTask (mProgressBar.setVisibility(View.VISIBLE);)中可见时,我有一个 NullPointerException 。我不知道出了什么问题!谢谢大家!

 <ProgressBar 
    android:id="@+id/pb_featured_game_progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    />

public class MainActivity extends Activity {

... 
private ProgressBar mProgressBar;



@Override
protected void onCreate(Bundle savedInstanceState) {

new FetchGamesTask().execute();


private class FetchGamesTask extends
        AsyncTask<Integer, Integer, List<GameInfo>> {


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub

        super.onPreExecute();

        mProgressBar = (ProgressBar)findViewById(R.id.pb_featured_game_progress);
        mProgressBar.setVisibility(View.VISIBLE);
    }

1 个答案:

答案 0 :(得分:4)

在调用FetchGamesTask上的执行之前,应将主视图与xml文件关联。

喜欢这个

protected void onCreate(Bundle savedInstanceState) {
// the OS will inflate the main_activity.xml
// file and use it for this activity
setContentView(R.layout.main_activity);
new FetchGamesTask().execute();