Android FindViewById返回null(ProgressBar)

Android FindViewById返回null(ProgressBar),android,android-layout,android-progressbar,findviewbyid,Android,Android Layout,Android Progressbar,Findviewbyid,我在StackOverflow上搜索了很多,在一些建议清理项目以解决此问题的评论中,该问题出现在适配器内部的asynctask类中,因此: 从eclipse目录(gen/bin)中删除该项目,然后再次导入 我删除了导入,清理了项目,按ctrl+O(再次导入)并再次编译了项目 没用 我得到此错误的部分代码: protected void onPreExecute() { ImageView img = (ImageView) mView.findViewById(R.id.gri

我在StackOverflow上搜索了很多,在一些建议清理项目以解决此问题的评论中,该问题出现在适配器内部的asynctask类中,因此:

  • 从eclipse目录(gen/bin)中删除该项目,然后再次导入
  • 我删除了导入,清理了项目,按ctrl+O(再次导入)并再次编译了项目
  • 没用

    我得到此错误的部分代码:

    protected void onPreExecute() {
    
            ImageView img = (ImageView) mView.findViewById(R.id.grid_action_button);
    
            if(img == null){
                Log.d(TAG, "img null");
            }else{
                Log.d(TAG, "img not null");
            }
    
            ProgressBar pBar = (ProgressBar) mView.findViewById(R.id.grid_progress_bar);
    
            if(pBar == null){
                Log.d(TAG, "pBar null");
            }else{
                Log.d(TAG, "pBar not null");
            }
    
        }
    
    最有趣的是,imageview工作正常,但是
    pBar
    变量的返回值为空

    Logcat结果:

    04-02 15:46:00.734: D/Adapter(10842): img not null
    04-02 15:46:00.734: D/Adapter(10842): pBar null
    
    griview布局的My custon适配器(mobile.xml):

    My
    getview()
    内部适配器类:

    public View getView(int position, View convertView, ViewGroup parent) {
    
        View gridView = convertView;
    
        if (gridView == null) {
    
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.mobile, null);
    
                ... more ...
    

    谢谢

    也许这对某人有用
    在从firebase获取数据之后,我使用了某种异步方法,我必须访问主屏幕上的progressbar。但是
    findViewById
    一直返回null

    最后我这样解决了:

        AppCompatActivity mainActivity = (AppCompatActivity)context;
        mainActivity.setContentView(R.layout.activity_main);
        ProgressBar loadPollsProgressBar = (ProgressBar) mainActivity.findViewById(R.id.loadPollsProgressBar);
    

    代码中的mView是什么?在AsyncTask构造函数中初始化
    mView
    。这是由
    imageButon.setOnClickListener的
    onClick(视图v)
    提供的。如果视图
    v
    仅显示在按钮上,这就是问题所在!对这似乎是问题所在谢谢你,你真的帮助我看到了问题所在。我找到了更好的解决办法!
    public View getView(int position, View convertView, ViewGroup parent) {
    
        View gridView = convertView;
    
        if (gridView == null) {
    
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.mobile, null);
    
                ... more ...
    
        AppCompatActivity mainActivity = (AppCompatActivity)context;
        mainActivity.setContentView(R.layout.activity_main);
        ProgressBar loadPollsProgressBar = (ProgressBar) mainActivity.findViewById(R.id.loadPollsProgressBar);