2011-09-03 37 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