{"id":7214,"date":"2014-05-28T02:20:19","date_gmt":"2014-05-28T02:20:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/28\/android-vertical-tab-layout-collection-of-common-programming-errors\/"},"modified":"2014-05-28T02:20:19","modified_gmt":"2014-05-28T02:20:19","slug":"android-vertical-tab-layout-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/28\/android-vertical-tab-layout-collection-of-common-programming-errors\/","title":{"rendered":"Android Vertical Tab Layout-Collection of common programming errors"},"content":{"rendered":"<p>I keep getting this error on running but no compile errors? here is the logcat and code for main.<\/p>\n<pre><code>05-25 12:29:25.543: W\/dalvikvm(384): threadid=1: thread exiting with uncaught exception (group=0x4001d800)\n05-25 12:29:25.582: E\/AndroidRuntime(384): FATAL EXCEPTION: main\n05-25 12:29:25.582: E\/AndroidRuntime(384): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tablayout\/com.example.tablayout.TablayoutActivity}: java.lang.NullPointerException\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.os.Handler.dispatchMessage(Handler.java:99)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.os.Looper.loop(Looper.java:123)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.app.ActivityThread.main(ActivityThread.java:4627)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at java.lang.reflect.Method.invokeNative(Native Method)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at java.lang.reflect.Method.invoke(Method.java:521)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at dalvik.system.NativeStart.main(Native Method)\n05-25 12:29:25.582: E\/AndroidRuntime(384): Caused by: java.lang.NullPointerException\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.app.Activity.setContentView(Activity.java:1647)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.app.TabActivity.ensureTabHost(TabActivity.java:114)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.app.TabActivity.getTabHost(TabActivity.java:136)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at com.example.tablayout.TablayoutActivity.(TablayoutActivity.java:15)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at java.lang.Class.newInstanceImpl(Native Method)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at java.lang.Class.newInstance(Class.java:1429)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)\n05-25 12:29:25.582: E\/AndroidRuntime(384):  ... 11 more\n\n\n\n\n\n\n    \n    \n     \n    \n    \n        \n            \n            \n            \n     \n           \n    \n    \n    \n<\/code><\/pre>\n<p>here is the main.java file, this way you can see how it was a global variable and where i removed it from. Also added the buttons globally instead of locally to that one spot.<\/p>\n<pre><code>    import android.app.TabActivity;\n    import android.content.Intent;\n   import android.os.Bundle;\n   import android.view.View;\n   import android.widget.Button;\n   import android.widget.TabHost;\n   import android.widget.TabHost.TabSpec;\n\n   public class TablayoutActivity extends TabActivity {\n    \/** Called when the activity is first created. *\/\n\nButton artistButton, songButton, videosButton;\nTabHost tabHost = getTabHost();\n\n@Override\n     public void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.main);\n\n    \/\/TabHost tabHost = getTabHost();\n\n    \/\/ Tab for Photos\n    TabSpec photospec = tabHost.newTabSpec(\"Photos\");\n    \/\/ setting Title and Icon for the Tab\n    photospec.setIndicator(\"Photos\",   getResources().getDrawable(R.drawable.icon_photos_tab));\n    Intent photosIntent = new Intent(this, PhotosActivity.class);\n    photospec.setContent(photosIntent);\n\n    \/\/ Tab for Songs\n    TabSpec songspec = tabHost.newTabSpec(\"Songs\");\n    songspec.setIndicator(\"Songs\", getResources().getDrawable(R.drawable.icon_songs_tab));\n    Intent songsIntent = new Intent(this, SongsActivity.class);\n    songspec.setContent(songsIntent);\n\n    \/\/ Tab for Videos\n    TabSpec videospec = tabHost.newTabSpec(\"Videos\");\n    videospec.setIndicator(\"Videos\",         getResources().getDrawable(R.drawable.icon_videos_tab));\n    Intent videosIntent = new Intent(this, VideosActivity.class);\n    videospec.setContent(videosIntent);\n\n    \/\/ Adding all TabSpec to TabHost\n    tabHost.addTab(photospec); \/\/ Adding photos tab\n    tabHost.addTab(songspec); \/\/ Adding songs tab\n    tabHost.addTab(videospec); \/\/ Adding videos tab\n\n\n\n\n   }\n\n    public void tabHandler(View target){\n    artistButton.setSelected(false);\n    videosButton.setSelected(false);\n    songButton.setSelected(false);\n    if(target.getId() == R.id.artist_id){\n        tabHost.setCurrentTab(0);\n        artistButton.setSelected(true);\n    } else if(target.getId() == R.id.album_id){\n        tabHost.setCurrentTab(1);\n        videosButton.setSelected(true);\n    } else if(target.getId() == R.id.song_id){\n        tabHost.setCurrentTab(2);\n        songButton.setSelected(true);\n    }\n   };\n\n    }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I keep getting this error on running but no compile errors? here is the logcat and code for main. 05-25 12:29:25.543: W\/dalvikvm(384): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 05-25 12:29:25.582: E\/AndroidRuntime(384): FATAL EXCEPTION: main 05-25 12:29:25.582: E\/AndroidRuntime(384): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tablayout\/com.example.tablayout.TablayoutActivity}: java.lang.NullPointerException 05-25 12:29:25.582: E\/AndroidRuntime(384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 05-25 12:29:25.582: E\/AndroidRuntime(384): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) [&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-7214","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7214","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=7214"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7214\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}