2014-02-24 270 views
1

我想在Java類中的Android中創建佈局。我需要用戶1之間引入一個號碼20,這創造用戶具有chosen.I要創建動態按鈕butttons的號碼,我有這樣的代碼:動態按鈕Android佈局

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); 
} 

我有一個錯誤的與星號

+0

什麼是具體的錯誤信息? NPE? –

+0

是纏着贓物,這是,但現在我有另一個問題,我動態創建的3個按鈕是垂直佈局,現在我已經將線性佈局更改爲水平,他們不會出現你正在發生什麼? – user3232094

回答

1

我敢肯定這是因爲你有setContentView(R.layout.panel_resuelto);onCreate的末尾。把它放在一開始,否則findViewById將無法​​正常工作。我猜你得到一個空指針異常,因爲LinearLayout不能成功創建,因爲內容視圖還沒有設置。

+1

更具體地說,在這種情況下創建'setContentView'後,'findViewById'將不起作用。 –

+0

是纏着贓物,但現在我有另一個問題,我動態創建的3個按鈕是垂直佈局,現在我已經將線性佈局更改爲水平,並且他們不會出現你正在發生的事情嗎? – user3232094

+0

我真的不知道。我會建議創建一個新的問題(使用完整的代碼)。 –