2014-06-18 55 views
0

我的Android應用程序有問題。我想通過這樣做,創建一個新的觀點:關於繼承視圖的setLayoutParams

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    LinearLayout l = (LinearLayout)findViewById(R.id.mainActivity); 
    f = new InheritedView(this); 
    f.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); 
    l.addView(f); 
} 

隨着

public class InheritedView extends ImageView implements AbstractView 

public InheritedView(Context context) { 
    super(context); 
} 

但是,當我測試,執行setLayoutParams當我的應用程序崩潰與java.lang.StackOverflowError的(..)。 但是,下面的工作:

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

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    LinearLayout l = (LinearLayout)findViewById(R.id.mainActivity); 
    f = new ImageView(this); 
    f.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); 
    l.addView(f); 
} 

logcat的文件

致命異常:主 工藝:fr.cameleoz.nemo,PID:30304 java.lang.StackOverflowError的 在fr.cameleoz.nemo。 business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) at fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) at fr.cameleoz。 nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32)(繼承視圖.java:32) (繼承的視圖)。在32位客戶端上, ...) at [012]你能解釋我爲什麼嗎? 謝謝

+0

您是否嘗試切換addView和setLayoutParams行? – Blackbelt

+0

我有。它崩潰,因爲缺乏必要的參數,我猜... –

+0

有一些東西在logcat中出現。應用程序不會無緣無故地崩潰。 –

回答

0

根據堆棧跟蹤,你的InheritedView覆蓋setLayoutParams()和重寫的方法調用自己。

每個嵌套的方法調用佔用一些堆棧空間,而這個無限遞歸只在堆棧用完時被終止。

+0

真的!我從未想過......非常感謝! –