初始化时,自定义视图返回NULL

时间:2017-01-05 01:57:00

标签: java android android-view android-gridlayout

我想创建一个自定义的可重用视图,以便在gridlayout中显示。

我似乎无法初始化视图。它总是在我的Activity中返回null

任何人都可以帮助我理解我做错了什么吗?我觉得它在我的Tile构造函数中。

我的活动:

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

Activity的XML的GridLayout部分:

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

我的可重用视图:

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

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>

我得到的错误:

  

尝试调用虚方法'void   android.widget.TextView.setText(java.lang.CharSequence)'为null   com.mycompany.myapp.Views.TileView.setTextTitle中的对象引用   (TileView.java:77)

2 个答案:

答案 0 :(得分:2)

问题是由于:

var filterTarget = document.querySelectorAll(".hubCheck input[type="checkbox"]");
var filters = document.querySelectorAll("#filters li");

for (var i = 0; i < filters.length; i++){
    filters[i].addEventListener("click", filterItems, false);
    filters[i].checked = true;
}
function filterItems(e){
    var clickedItem = e.target;
    if (clickedItem.checked == true) {
    document.getElementById("checkbox").checked = true;
    } else if (clickedItem.checked == false) {
        document.getElementById("checkbox").checked = false;
    } else {

    }
}

线。 tile.setTextTitle("CALLS"); mTitle,因为在初始化TextView的任何地方都不会调用null方法。

在调用TileView()方法之前调用TileView()

setTextTitle

或在构造函数中调用tile.TileView(); tile.setTextTitle("CALLS"); 方法:

TileView

答案 1 :(得分:0)

尝试将TileView()中的代码移动到TileView(上下文上下文)

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