简体   繁体   English

FindViewById 返回 null (ProgressBar)

[英]FindViewById returning null (ProgressBar)

I searched a lot on StackOverflow and, in several comments is recommended to clean the project to solve this issue, the problem ocours in asynctask class inside Adapter, so:我在 StackOverflow 上搜索了很多,在几条评论中建议清理项目以解决此问题,问题出现在 Adapter 内部的 asynctask 类中,因此:

  1. removed the project from eclipse, directories (gen / bin) and imported again从eclipse,目录(gen / bin)中删除项目并再次导入
  2. I removed the imports, cleaned the project, ctrl+O (to import again) and compiled the project again我删除了导入,清理了项目,ctrl+O(再次导入)并再次编译项目

Did not work!不工作!

Part of code where I get this error:我收到此错误的部分代码:

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

    }

The most interesting is that, the imageview works normally, but the return of pBar variable is null.最有趣的是,imageview正常工作,但是pBar变量返回null。

Logcat result: Logcat 结果:

04-02 15:46:00.734: D/Adapter(10842): img not null
04-02 15:46:00.734: D/Adapter(10842): pBar null

My custon adapter to griview layout (mobile.xml):我的自定义适配器到 griview 布局(mobile.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="30dp" >

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="140dp"
        android:layout_height="186dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:orientation="vertical"
        >
        <!-- I have some TextViews here -->

    <ImageView
            android:id="@+id/grid_action_button"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/btn_download"
            android:clickable="true" >
            </ImageView>

    <ProgressBar             
        android:id="@+id/grid_progress_bar"
        android:layout_width="100dp"
        android:layout_height="10dp"
        style="style/Widget.ProgressBar.Horizontal"
    />

</LinearLayout>
</LinearLayout>

The setContentView is called in Main class:在 Main 类中调用setContentView

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gridView = (GridView) findViewById(R.id.gridview);
}

My getview() inside Adapter class:我在 Adapter 类中的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 ...

Thanks谢谢

Perhaps this will be useful to someone.也许这对某人有用。
I was in some asynchronous method, after getting data from firebase, and I had to access the progressbar on the main screen.从 firebase 获取数据后,我采用了某种异步方法,我不得不访问主屏幕上的进度条。 But findViewById kept returning null.但是findViewById一直返回 null。

Finally I solved it like this:最后我是这样解决的:

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

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

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