{"id":2401,"date":"2022-08-30T15:24:33","date_gmt":"2022-08-30T15:24:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/android-programmatically-adding-relativelayout-to-to-tablelayout-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:24:33","modified_gmt":"2022-08-30T15:24:33","slug":"android-programmatically-adding-relativelayout-to-to-tablelayout-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-programmatically-adding-relativelayout-to-to-tablelayout-collection-of-common-programming-errors\/","title":{"rendered":"Android programmatically adding RelativeLayout to to TableLayout-Collection of common programming errors"},"content":{"rendered":"<p>So, I decided to learn to write Android apps. I prefer learning by writing code so I setup my environment and dived in to Android!<\/p>\n<p>And hit my head \ud83d\ude42<\/p>\n<p>I am trying to add import RelativeLayout as TableRows to a Table view.<\/p>\n<p>This is my table layout which I want to re-use,<\/p>\n<pre><code>    \n\n\n\n<\/code><\/pre>\n<p>I included it i my my main activity with<\/p>\n<pre><code>\n    \n\n\n\n<\/code><\/pre>\n<p>Then, I created my Row with RelativeLayout<\/p>\n<pre><code>\n \n\n\n\n\n\n\n\n\n\n\n \n<\/code><\/pre>\n<p>Now, I am trying this<\/p>\n<pre><code> public class HomeActivity extends Activity {\n\/** Called when the activity is first created. *\/\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.home_activity);\n\n\n    TableLayout tbl = (TableLayout)findViewById(R.id.homeActivityUploadList);\n    RelativeLayout row = (RelativeLayout)findViewById(R.layout.upload_list_table_row_relative);\n\n    tbl.addView(row);\n\n\n}\n<\/code><\/pre>\n<p>But, i get NullPointerException. It can&#8217;t find<\/p>\n<pre><code>RelativeLayout row = (RelativeLayout)findViewById(R.layout.upload_list_table_row_relative);\n<\/code><\/pre>\n<p>I&#8217;ve also tried<\/p>\n<pre><code>RelativeLayout row = (RelativeLayout)findViewById(R.id.upload_list_table_row_relative);\n<\/code><\/pre>\n<p>What am I doing wrong?<\/p>\n<p>Thanks<\/p>\n<h2>Log output<\/h2>\n<pre><code>    01-24 11:30:15.757: D\/dalvikvm(1625): GC_EXTERNAL_ALLOC freed 46K, 53% free    2539K\/5379K, external 1625K\/2137K, paused 68ms\n<\/code><\/pre>\n<p>01-24 11:30:15.877: D\/dalvikvm(1625): GC_EXTERNAL_ALLOC freed 1K, 53% free 2538K\/5379K, external 2250K\/2810K, paused 58ms 01-24 11:30:15.967: D\/AndroidRuntime(1625): Shutting down VM 01-24 11:30:15.967: W\/dalvikvm(1625): threadid=1: thread exiting with uncaught exception (group=0x40015560) 01-24 11:30:15.977: E\/AndroidRuntime(1625): FATAL EXCEPTION: main 01-24 11:30:15.977: E\/AndroidRuntime(1625): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.<strong><em>*<\/em><\/strong>.HomeActivity}: java.lang.NullPointerException 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.os.Handler.dispatchMessage(Handler.java:99) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.os.Looper.loop(Looper.java:123) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.app.ActivityThread.main(ActivityThread.java:3683) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at java.lang.reflect.Method.invokeNative(Native Method) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at java.lang.reflect.Method.invoke(Method.java:507) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at dalvik.system.NativeStart.main(Native Method) 01-24 11:30:15.977: E\/AndroidRuntime(1625): Caused by: java.lang.NullPointerException 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.view.ViewGroup.addView(ViewGroup.java:1821) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.widget.TableLayout.addView(TableLayout.java:403) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.view.ViewGroup.addView(ViewGroup.java:1808) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.widget.TableLayout.addView(TableLayout.java:394) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at com.<strong>*<\/strong>.onCreate(HomeActivity.java:23) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-24 11:30:15.977: E\/AndroidRuntime(1625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 01-24 11:30:15.977: E\/AndroidRuntime(1625): &#8230; 11 more<\/p>\n<pre><code>    android:ellipsize=\n<\/code><\/pre>\n<ol>\n<li>\n<p>you must use LayoutInflater<\/p>\n<pre><code>LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);\nRelativeLayout  row = (RelativeLayout ) layoutInflater.inflate(R.layout.upload_list_table_row_relative, null);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-05 22:46:04. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>So, I decided to learn to write Android apps. I prefer learning by writing code so I setup my environment and dived in to Android! And hit my head \ud83d\ude42 I am trying to add import RelativeLayout as TableRows to a Table view. This is my table layout which I want to re-use, I included [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2401","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2401"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2401\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}