Frage

I am trying to inflate a custom view object into my Layout XML, but I am getting an inflate exception, can someone help me?

Here is my Layout XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

<ViewGroup
    android:id="@+id/desenha_foto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_above="@+id/button_capture"/>

<Button
    android:id="@+id/button_capture"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="snapIt" 
    android:text="@string/Capture"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

Here is my code, where I try to inflate the ViewGroup:

public class Desenha_Foto extends ActionBarActivity {

    private DesenhaFotoView draw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bitmap bitmap = Global.getInstance().getBitmap();
        draw = new DesenhaFotoView(this, bitmap);

        setContentView(R.layout.activity_desenha_foto);

        final ViewGroup viewGroup = (ViewGroup) findViewById(R.id.desenha_foto);
        viewGroup.addView(draw);

        Global.getInstance().getProgressDialog().dismiss();
    }

}
War es hilfreich?

Lösung

You can't use a ViewGroup tag in the xml layout because ViewGroup is an abstract class. You either use one of ViewGroup's children(LinearLayout, RelativeLayout etc) or you use a custom implementation(a class that extends ViewGroup). You have your own view, DesenhaFotoView use that instead of the ViewGroup.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top