简体   繁体   中英

Can't inflate custom view

I have a custom view that is called BarGraph , and when I try to inflate it, it wont.

public class BarGraph extends View {
public BarGraph(Context context) {
    super(context);
    mContext = context;
    // inflateView();
}

public BarGraph(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    // inflateView();
}
private void inflateView() {
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //This line is wrong, can't inflate BarGraph
    View v = inflater.inflate(R.layout.bar_graph,this, true);
    hsv = (HorizontalScrollView) v.findViewById(R.id.hsv_bar_graph);
}

The error is :

The method inflate(int, ViewGroup, boolean) in the type LayoutInflater is not applicable for the arguments (int, BarGraph, boolean)

Please look on it

inflater.inflate(R.layout.list_layout, null); // no parent


inflater.inflate(R.layout.schedule_layout, (ViewGroup) lst_item_view, false); // with parent` preserving LayoutParams

please check the logs and work accordingly, surely yours problem will be solved.

The second parameter must be a ViewGroup and your class is extending a View . It needs a ViewGroup because the method addView is defined for ViewGroup not for View (that is the boolean attachToRoot does)

try this:

View v = inflater.inflate(R.layout.bar_graph,(ViewGroup) this.getParent(), true);

or set ViewGroup to null.

将您的自定义类扩展到ViewSwitcher例如: customView extends ViewSwitcher这将解决您的问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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