简体   繁体   English

请帮我在android中的自定义布局

[英]Please help me me in custom layout in android

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 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>

Demo.java 演示程序

public class Demo extends Activity {

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


}

activity_demo.xml 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>

You can't put an activity in a layout. 您不能将活动放置在布局中。 Only views or view groups. 仅视图或视图组。

The inflator uses the two argument constructor when it creates an instance of your view class. 充气机在创建视图类的实例时使用两个参数构造函数。 You must provide 您必须提供

public MainActivity(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

Since Demo is your Activity, define it in Android manifest.xml file using the package name: com.example.demo.Demo. 由于Demo是您的活动,因此请使用包名称com.example.demo.Demo在Android manifest.xml文件中对其进行定义。 And why are you adding framelayout inside frameLayout use only one , your custom framelayout. 以及为什么要在frameLayout中添加framelayout只使用一个,即自定义framelayout。 and define the xml as: 并将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"
 />

//hope it works //希望它起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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