クラスにはデフォルトのTextView
コンストラクターがありません。3 つの利用可能なコンストラクターは次のとおりです。
TextView(Context context)
TextView(Context context, AttributeSet attrs)
TextView(Context context, AttributeSet attrs, int defStyle)
Button
クラスでも同じこと:
public Button (Context context)
public Button (Context context, AttributeSet attrs)
public Button (Context context, AttributeSet attrs, int defStyle)
Context
すべての UI (の子孫View
) コントロールをインスタンス化するには、少なくとも変数を渡す必要があります。
次の方法でコードを変更します。
Context ctx = ...;
Class<?> components[] = {TextView.class, Button.class };
Constructor<?> ctor = components[nIndexOfControl].getConstructor(Context.class);
Object obj = ctor.newInstance(ctx);