开发者

How to resolve the error: Java class cannot be instantiated in XML layout in android?

public class BottomToolbar extends RelativeLayout {
private Spinner circleSpinner;
private ImageButton operatorImageButton;
private ToggleButton conntypeToggleButton;
private Context context;

public BottomToolbar(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.toolbar, this);
    this.context = context;
    circleSpinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            context, R.array.circlesarray,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    circleSpinner.setAdapter(adapter);
}}

The above code creates a toolbar whose layout is defined in XML. But in XML view it shows the following error:

The following classes could not be instantiated: - com.example.BottomToolbar

If I comment these lines the error goes:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        context, R.array.circlesarray,
        android.R.layout.simple_s开发者_如何学Pythonpinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
circleSpinner.setAdapter(adapter);

Please help, I suspect there is something wrong with "context" in Array Adapter declaration.

--Thanks in advance


Try

public BottomToolbar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initView(context);
}

public BottomToolbar(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context);
}

public BottomToolbar(Context context) {
    super(context);
    initView(context);
}

public void initView(Context context) {
    LayoutInflater layoutInflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.toolbar, this);
    this.context = context;
    circleSpinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        context, R.array.circlesarray,
        android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    circleSpinner.setAdapter(adapter);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜