Android 使用XML元素以编程方式创建布局

Android 使用XML元素以编程方式创建布局,android,xml,view,Android,Xml,View,我已经按程序对我的一个布局进行了编程。当我试图用XML实现它时,我无法让它工作。它与NullPointerException一起崩溃,我真的不知道为什么 这是我的XML布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" an

我已经按程序对我的一个布局进行了编程。当我试图用XML实现它时,我无法让它工作。它与NullPointerException一起崩溃,我真的不知道为什么

这是我的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"
android:orientation="vertical"
tools:context=".DisplayMessageActivity" >

<ImageView
    android:id="@+id/canal_1"
    android:contentDescription="@string/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:onClick="canal1_Click"
    android:src="@drawable/pestanya_seleccionada" />

</RelativeLayout>
它在
relativeLayout.addView(canal1)崩溃

我不知道这为什么会失败。在我看来,一切都会好起来的

谢谢你的阅读,希望你能帮助我;)

亲切问候,,
劳尔

您尚未将xml布局的内容设置到屏幕,您正在查找ImageView的id。这导致了NPE

 canal1 = (ImageView) findViewById(R.id.canal_1);
上面的语句将导致nullpointerexception,因为您尚未设置布局,并且正在尝试查找xml文件中定义的表单id

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activty_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
//add other ui elements to the root layout ie  RelativeLayout
}

<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"
android:id="@+id/relativeLayout"// relative layout id
android:orientation="vertical"
tools:context=".DisplayMessageActivity" >
<ImageView
android:id="@+id/canal_1"
android:contentDescription="@string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="canal1_Click"
android:src="@drawable/pestanya_seleccionada" />
</RelativeLayout>
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.Activity_main);
RelativeLayout rl=(RelativeLayout)findViewById(R.id.RelativeLayout);
//将其他ui元素添加到根布局ie RelativeLayout
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activty_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
//add other ui elements to the root layout ie  RelativeLayout
}

<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"
android:id="@+id/relativeLayout"// relative layout id
android:orientation="vertical"
tools:context=".DisplayMessageActivity" >
<ImageView
android:id="@+id/canal_1"
android:contentDescription="@string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="canal1_Click"
android:src="@drawable/pestanya_seleccionada" />
</RelativeLayout>