Reputation: 2571
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 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);
}
}
Upvotes: 0
Views: 456
Reputation: 26
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>
Upvotes: 1
Reputation: 47809
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:
If you can't root out the problem with the two techniques above, please post more details.
Upvotes: 0