简体   繁体   English

ProgressBar的AsyncTask的onPreExcute中的NullpointerException

[英]NullpointerException in onPreExcute of AsyncTask for ProgressBar

I had a NullPointerException when I try to set ProgessBar to visible in onPreExecute AsyncTask (mProgressBar.setVisibility(View.VISIBLE);) . 当我尝试将ProgessBar设置为在onPreExecute AsyncTask (mProgressBar.setVisibility(View.VISIBLE);)可见时,我遇到了NullPointerException I don't know what went wrong! 我不知道出了什么问题! Thanks you all! 谢谢大家!

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

Before you call the execute on your FetchGamesTask , you should associate the main view with the xml file. FetchGamesTask上调用execute之前,应将主视图与xml文件关联。

Like this 像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM