开发者

Table layout in android

I tried the following code for table layout in android without using xml file. But i didn't get my screen on android emulator but getting the errror as "the application has stopped upexpectedly.Please try again."

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import开发者_StackOverflow社区 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);
}

}


I'm not getting a force close when I run this code, but you are missing something.

I tried adding the following, but did not see any text:

tv.setText("Hello world!");

The problem is that when you call to tr.addView, you aren't setting any layout parameters on the new row. If you change the line to the following, the text appears:

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

Edit: Here's a thought on the force close, is Tablelayout correctly defined in your AndroidManifest.xml? You should have something like this.

<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>


well, I can't see any particular reason off the top of my head that this code would cause a force close. There's a few ways that you can troubleshoot this:

  1. Look at logcat to see what is the exception being thrown. This might give you an indication on what the problem is.
  2. Set the debugger to break when the exception is thrown. It's very simple to configure in the breakpoint window on the debug perspective. Here's a tutorial on doing so: http://blogs.bytecode.com.au/glen/2007/04/06/eclipse-tip--breakpoint-on-exception.html

If you can't root out the problem with the two techniques above, please post more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜