Custom view returns NULL when initialized

448 Views Asked by At

I want to create a custom reusable view to show in a gridlayout.

I can't seem to get the view to initialize. It always returns null in my Activity

Can anyone help me understand what I am doing wrong? I feel like it is in my Tile constructor.

My Activity:

private void addTile() {
       TileView tile = new TileView(this);
       tile.setTextTitle("CALLS");
       tile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tilePressed(v);
            }
       });

       mGridLayout.addView(tile);   
}

The GridLayout portion of the Activity's XML:

<GridLayout
    android:id="@+id/glTileArea"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/COLOR_LIGHT_GREEN"
    android:columnCount="2"
    android:rowCount="2" >

</GridLayout>

My reusable view:

public class TileView extends View{

        private View mView;
        private Context mContext;
        private TextView mTitle;


        public TileView(Context context) {
            super(context);
            this.mContext = context;
        }

        public View TileView(){
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            mView = inflater.inflate(R.layout.sample_tile_view,null);

            mTitle = (TextView) mView.findViewById(R.id.tvTitle);

            return mView;
        }

        public void setTextTitle(String text){
             mTitle.setText(text);
        }
}

The XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/COLOR_BLUE"
              android:orientation="vertical">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="TITLE"
        android:textAlignment="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/COLOR_WHITE"
        android:textStyle="bold"/>


</LinearLayout>

Error that I get:

Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.mycompany.myapp.Views.TileView.setTextTitle (TileView.java:77)

2

There are 2 best solutions below

6
On BEST ANSWER

Problem is due to:

tile.setTextTitle("CALLS");

line. mTitle is null because TileView() method is not called anywhere in which initializing TextView.

Either call TileView() before calling setTextTitle method:

tile.TileView();
tile.setTextTitle("CALLS");

or call TileView method in constructor :

public TileView(Context context) {
   super(context);
   this.mContext = context;
   this.TileView(); //<< call here
}
0
On

Try to move the code in TileView() to TileView(Context context)

public TileView(Context context) {
    super(context);
    this.mContext = context;
    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
    mView = inflater.inflate(R.layout.sample_tile_view,null);
    mTitle = (TextView) mView.findViewById(R.id.tvTitle); 
}