0
java.lang.RuntimeException: Unable to start activity `ComponentInfo{com.example.demo/com.example.demo.Demo}: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.component.MainActivity`
public class MainActivity extends FrameLayout {    
    public MainActivity(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

演示.java

public class Demo extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
    }


}

活动演示.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 >

  <com.example.component.MainActivity
    android:id="@+id/comp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
      />
</FrameLayout>
4

3 回答 3

0

You can't put an activity in a layout. Only views or view groups.

于 2013-01-19T08:46:12.267 回答
0

充气机在创建视图类的实例时使用两个参数构造函数。您必须提供

public MainActivity(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}
于 2013-01-19T08:46:50.390 回答
0

由于 Demo 是您的 Activity,因此在 Android manifest.xml 文件中使用包名定义它:com.example.demo.Demo。为什么你在 frameLayout 中添加 framelayout 只使用一个,你的自定义 framelayout。并将xml定义为:

<com.example.component.MainActivity
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/comp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 />

//希望它有效

于 2014-12-18T10:08:16.377 回答