0
我已經以編程方式編寫了我的佈局之一。當我嘗試在XML中實現它時,我無法使其工作。它崩潰與NullPointerException,我真的不知道爲什麼。使用XML元素以編程方式創建佈局
這是我的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>
而且我想要的是:
ImageView canal1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* layout prinicpal */
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
canal1 = (ImageView) findViewById(R.id.canal_1);
relativeLayout.addView(canal1);
setContentView(relativeLayout, rlp);
}
它崩潰的relativeLayout.addView(canal1);
我不知道爲什麼會失敗。在我腦子裏,一切都應該運行良好。
感謝您的閱讀,希望您能幫助我。)
親切的問候, 勞爾