• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps TableLayout declared in XML, not found in code.

Ojive

Lurker
Heya!

This has started to do my head in, and I can't figure this out.

After many searches on google, i found several examples of how to programmatically/dynamically add rows to a TableLayout.

My customers.xml file.
Code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="0,1"
                android:id="@+id/customers_table" >
</TableLayout>

My CustomersActivity.java:

Code:
setContentView(R.layout.customers);

later on:

TableLayout tl = (TableLayout)findViewById(R.id.customers_table);

My AndroidManifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.crmdeskapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_tab_support"
        android:label="@string/app_name" >
        <activity android:name=".CustomersActivity" android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar"
          android:screenOrientation="landscape">
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
                
    	</activity>
    	<activity android:name =".CustomerActivity">
    	  
    	</activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
Now, for some weird reason "findViewById" returns null. This is frustrating as hell since "customers_table" TableLayout is declared RIGHT THERE.

Am i missing something? Newbie here.
 
Where do you call setContentView(R.layout.customers)?

You should make sure to call it in the acitivity's onCreate() method.
 
Right. I think i found the source of my problem. Orientation.

I started experimenting with orientations, and layouts for these. Apparently, the "customers.xml" had to be identical (or the name of the layout atleast, had to be identical). Problem solved.

.....i think :D It runs atleast.
 
Back
Top Bottom