2011-09-03 59 views
0

我嘗試了以下代碼在android中的表格佈局,而不使用xml文件。但是我沒有在android模擬器上得到我的屏幕,但得到了錯誤,因爲「該應用程序已經停止了精確的操作,請再試一次。」在android中的表格佈局

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class Tablelayout extends Activity{ 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 

    LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); 

    TextView tv=new TextView(this); 
    tv.setLayoutParams(params); 
    tv.setTextColor(Color.RED); 

    TableRow tr=new TableRow(this); 
    tr.addView(tv); 

    TableLayout tl=new TableLayout(this); 
    TableLayout.LayoutParams layoutparams=new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
    tl.addView(tr); 

    this.addContentView(tl, layoutparams); 
} 

}

+0

我建議你使用調試器運行你的應用程序,看看logcat告訴你什麼。錯誤消息通常非常具有描述性。 –

+0

Logcat顯示運行時錯誤.... –

回答

1

當我運行此代碼時,我沒有收到強制關閉,但您錯過了某些內容。

我嘗試添加以下,但並沒有看到任何文字:

tv.setText("Hello world!"); 

的問題是,當你打電話tr.addView,你是不是設置在新行的任何佈局參數。如果更改線以下,顯示的文字:

tr.addView(tv, new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

編輯:這裏有力閉合一想,是Tablelayout正確的AndroidManifest.xml中定義?你應該有這樣的事情。

<application android:label="@string/app_name"> 
    <activity android:name=".Tablelayout" android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 
0

好,我看不出有什麼特別的原因,我的頭頂部,該代碼將導致強制關閉。有幾種方法可以排除故障:

  1. 看看logcat,看看拋出的異常是什麼。這可能會告訴你問題是什麼。
  2. 設置調試器在拋出異常時中斷。在調試透視圖的斷點窗口中配置非常簡單。這裏有一個教程如下:http://blogs.bytecode.com.au/glen/2007/04/06/eclipse-tip--breakpoint-on-exception.html

如果你不能解決上述兩種技術的問題,請發佈更多的細節。

+0

如何在Android中過濾表格佈局?我有一個表格佈局。我想從這個表格佈局搜索數據。請幫幫我。 – ANIL