[英]dynamic buttons Android Layout
我想在Android中的Java類中創建布局。 我需要用戶輸入一個介於1到20之間的數字,然后創建用戶選擇的對接數量。我要創建動態按鈕,並獲得以下代碼:
package com.example.nuevo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
public class ControladorResuelto extends Activity {
protected Controlador controlador;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
LinearLayout buttonsLayout = (LinearLayout)findViewById(R.id.linearlayoutUp);
int nMallas = controlador.getnMallas();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int i=0;i<nMallas;i++){
Button button = new Button(this);
button.setText("Buttin "+(i+1));
buttonsLayout.addView(button, layoutParams);******
}
setContentView(R.layout.panel_resuelto);
}
我在帶星號的行上有錯誤
我很確定這是因為您有setContentView(R.layout.panel_resuelto);
在onCreate
的末尾。 將其放在開頭,否則findViewById
將不起作用。 我猜您會收到一個空指針異常,因為由於尚未設置內容視圖,因此無法成功創建LinearLayout
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.