0

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:

  1. removed the project from eclipse, directories (gen / bin) and imported again
  2. I removed the imports, cleaned the project, ctrl+O (to import again) and compiled the project again

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.

Logcat result:

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):

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

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

My getview() inside Adapter class:

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

elirigobeli
  • 1,391
  • 2
  • 15
  • 22

1 Answers1

0

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. But findViewById kept returning 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);
B. Go
  • 1,436
  • 4
  • 15
  • 22
Amos
  • 1