Hello Tabwidget force closing


Last Updated:

  1. ajent

    ajent Member This Topic's Starter

    Joined:
    Jun 3, 2010
    Messages:
    7
    Likes Received:
    0
    I've followed the tutorial at Tab Layout | Android Developers
    and they seem to have fixed the issues that are coming up when i'm searching problems with it, like typo's, however eclipse shows no build errors at all. The problem is when I run it in emulation, I get a force close..

    With no errors in Eclipse and still being new to this language, i'm stuck.

    Here's an image of the tree:
    [​IMG]

    AlbumsActivity.java, ArtistsActivity.java, and SongsActivity.java:
    Code (Text):
    1. package com.hello;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.widget.TextView;
    6.  
    7. public class AlbumsActivity extends Activity {
    8.     public void onCreate(Bundle savedInstanceState) {
    9.         super.onCreate(savedInstanceState);
    10.  
    11.         TextView textview = new TextView(this);
    12.         textview.setText("This is the Artists tab");
    13.         setContentView(textview);
    14.     }
    15. }
    HelloTabWidget.java
    Code (Text):
    1. package com.hello;
    2.  
    3.  
    4. import android.app.TabActivity;
    5. import android.content.Intent;
    6. import android.content.res.Resources;
    7. import android.os.Bundle;
    8. import android.widget.TabHost;
    9.  
    10. public class HelloTabWidget extends TabActivity {
    11.  
    12.     /** Called when the activity is first created. */
    13.     @Override
    14.     public void onCreate(Bundle savedInstanceState) {
    15.         super.onCreate(savedInstanceState);
    16.         setContentView(R.layout.main);
    17.  
    18.         Resources res = getResources(); // Resource object to get Drawables
    19.         TabHost tabHost = getTabHost();  // The activity TabHost
    20.         TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    21.         Intent intent;  // Reusable Intent for each tab
    22.  
    23.         // Create an Intent to launch an Activity for the tab (to be reused)
    24.         intent = new Intent().setClass(this, ArtistsActivity.class);
    25.  
    26.         // Initialize a TabSpec for each tab and add it to the TabHost
    27.         spec = tabHost.newTabSpec("artists").setIndicator("Artists",
    28.                           res.getDrawable(R.drawable.ic_tab_artists))
    29.                       .setContent(intent);
    30.         tabHost.addTab(spec);
    31.  
    32.         // Do the same for the other tabs
    33.         intent = new Intent().setClass(this, AlbumsActivity.class);
    34.         spec = tabHost.newTabSpec("albums").setIndicator("Albums",
    35.                           res.getDrawable(R.drawable.ic_tab_albums))
    36.                       .setContent(intent);
    37.         tabHost.addTab(spec);
    38.  
    39.         intent = new Intent().setClass(this, SongsActivity.class);
    40.         spec = tabHost.newTabSpec("songs").setIndicator("Songs",
    41.                           res.getDrawable(R.drawable.ic_tab_songs))
    42.                       .setContent(intent);
    43.         tabHost.addTab(spec);
    44.  
    45.         tabHost.setCurrentTab(0);      
    46.     }
    47. }
    ic_tab_albums.xml, ic_tab_artists.xml & ic_tab_songs.xml:
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3.     <!-- When selected, use grey -->
    4.     <item android:drawable="@drawable/ic_tab_artists_grey"
    5.           android:state_selected="true" />
    6.     <!-- When not selected, use white-->
    7.     <item android:drawable="@drawable/ic_tab_artists_white" />
    8. </selector>
    Main.xml:
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:id="@android:id/tabhost"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent">
    6.     <LinearLayout
    7.         android:orientation="vertical"
    8.         android:layout_width="fill_parent"
    9.         android:layout_height="fill_parent"
    10.         android:padding="5dp">
    11.         <TabWidget
    12.             android:id="@android:id/tabs"
    13.             android:layout_width="fill_parent"
    14.             android:layout_height="wrap_content" />
    15.         <FrameLayout
    16.             android:id="@android:id/tabcontent"
    17.             android:layout_width="fill_parent"
    18.             android:layout_height="fill_parent"
    19.             android:padding="5dp" />
    20.     </LinearLayout>
    21. </TabHost>
    AndroidManifest.xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.       package="com.hello"
    4.       android:versionCode="1"
    5.       android:versionName="1.0">
    6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
    7.         <activity android:name=".HelloTabWidget"
    8.         android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    9.                   android:label="@string/app_name">
    10.                   <activity android:name=".AlbumsActivity"
    11.                   android:label="@string/app_name"
    12.                   android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    13.         </activity>
    14.   <activity android:name=".ArtistsActivity"
    15.                   android:label="@string/app_name"
    16.                   android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    17.         </activity>
    18.           <activity android:name=".SongsActivity"
    19.                   android:label="@string/app_name"
    20.                   android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    21.         </activity>
    22.        
    23.             <intent-filter>
    24.                 <action android:name="android.intent.action.MAIN" />
    25.                 <category android:name="android.intent.category.LAUNCHER" />
    26.             </intent-filter>
    27.         </activity>
    28.  
    29.     </application>
    30.     <uses-sdk android:minSdkVersion="3" />
    31.  
    32. </manifest>
    Thanks
     

    Advertisement
  2. ajent

    ajent Member This Topic's Starter

    Joined:
    Jun 3, 2010
    Messages:
    7
    Likes Received:
    0
    I've seemed to resolve the issue. The error was "E/AndroidRuntime( 226): Caused by: android.content.ActivityNotFoundException: U
    nable to find explicit activity class {com.hello/com.hello.ArtistsActivity}; have you declared this activity in your AndroidManifest.xml?"

    I noticed (in my screenshot), the 3 activities (tabs) I created were Under the main. So I moved ArtistsActivity Up, and viola! Issue fixed, unless I click the other tabs. So moving them up too resolved the issue.
    Thanks

    [​IMG]
     
  3. kivy

    kivy Member

    Joined:
    Jul 13, 2010
    Messages:
    17
    Likes Received:
    0
    Hi there, I am very new to Android and I have the same problem, but everything I try doesn't seem to work. The app just won't launch. It would be great if someone could help me. I am really starting to become frustrated because I need it for a project and I was told that this tutorial would help me, but I still got this error.


    Here is my code:

    The main java file GalleryView.java
    Code (Text):
    1. package com.mobilevideoeditor.moved;
    2.  
    3. import android.app.TabActivity;
    4. import android.content.Intent;
    5. import android.content.res.Resources;
    6. import android.os.Bundle;
    7. import android.widget.TabHost;
    8.  
    9. public class GalleryView extends TabActivity {
    10.     /** Called when the activity is first created. */
    11.     @Override
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.main);
    15.        
    16.         Resources res = getResources(); // Resource object to get Drawables
    17.         TabHost tabHost = getTabHost();  // The activity TabHost
    18.         TabHost.TabSpec spec;  // Reusable TabSpec for each tab
    19.         Intent intent;  // Reusable Intent for each tab
    20.  
    21.         // Create an Intent to launch an Activity for the tab (to be reused)
    22.         intent = new Intent().setClass(this, EditGalleryView.class);
    23.  
    24.         // Initialize a TabSpec for each tab and add it to the TabHost
    25.         spec = tabHost.newTabSpec("edit").setIndicator("Edit",
    26.                           res.getDrawable(R.drawable.ic_tab_edit))
    27.                       .setContent(intent);
    28.         tabHost.addTab(spec);
    29.  
    30.         // Do the same for the other tabs
    31.         intent = new Intent().setClass(this, ShareGalleryView.class);
    32.         spec = tabHost.newTabSpec("share").setIndicator("Share",
    33.                           res.getDrawable(R.drawable.ic_tab_share))
    34.                           .setContent(intent);
    35.         tabHost.addTab(spec);
    36.  
    37.         tabHost.setCurrentTab(0);
    38.     }
    39. }
    The code for ShareGalleryView.java and EditGalleryView.java looks like this:

    Code (Text):
    1. package com.mobilevideoeditor.moved;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.widget.TextView;
    6.  
    7.  
    8. public class EditGalleryView extends Activity {
    9.     public void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.  
    12.         TextView textview = new TextView(this);
    13.         textview.setText("This is the Edit tab!");
    14.         setContentView(textview);
    15.     }
    16. }
    The main.xml:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7. <TextView  
    8.     android:layout_width="fill_parent"
    9.     android:layout_height="wrap_content"
    10.     android:text="@string/hello"
    11.     />
    12. </LinearLayout>
    13.  
    The ic_tab_share.xml and ic_tab_edit.xml:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3.     <!-- When selected, use ic_tab_share (grey icon) -->
    4.     <item android:drawable="@drawable/ic_tab_share"
    5.           android:state_selected="true" />
    6.     <!-- When not selected, use ic_tab_share_unselected (white icon)-->
    7.     <item android:drawable="@drawable/ic_tab_share_unselected" />
    8. </selector>
    The Manifest.xml:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.       package="com.mobilevideoeditor.moved"
    4.       android:versionCode="1"
    5.       android:versionName="1.0">
    6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
    7.            <activity android:name=".GalleryView" android:label="@string/app_name"
    8.                   android:theme="@android:style/Theme.NoTitleBar">  
    9.             <intent-filter>
    10.                 <action android:name="android.intent.action.MAIN" />
    11.                 <category android:name="android.intent.category.LAUNCHER" />
    12.             </intent-filter>
    13.             <activity android:name=".ShareGalleryView" android:label="@string/app_name"
    14.                       android:theme="@android:style/Theme.NoTitleBar"> </activity>
    15.                       <activity android:name=".EditGalleryView" android:label="@string/app_name"
    16.                   android:theme="@android:style/Theme.NoTitleBar"> </activity>  
    17.         </activity>
    18.        
    19.  
    20.     </application>
    21.  
    22.  
    23. </manifest>
    Thank you very much in advance. :)
     
  4. Mao

    Mao New Member

    Joined:
    Jul 27, 2010
    Messages:
    1
    Likes Received:
    0
    Your AndroidManifest.xml configuration is not corrected. You should not set note activity in node activity. You have to use as below:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nokorit"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".HelloTabWidget"
    android:label="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    <activity android:name=".AlbumsActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    </activity>
    <activity android:name=".ArtistsActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    </activity>
    <activity android:name=".SongsActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    </activity>

    </application>
     
  5. Jyothi

    Jyothi New Member

    Joined:
    Jul 26, 2011
    Messages:
    1
    Likes Received:
    0
    Hi,

    If u get the force close error in HelloTabWidget follow these corrections

    1.Modify HelloTaabActivity.java as follows

    package com.hello; import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.widget.TabHost; public class HelloTabWidget extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Resusable TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this, ArtistsActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(R.id.textview1); tabHost.addTab(spec); // Do the same for the other tabs intent = new Intent().setClass(this, AlbumsActivity.class); spec = tabHost.newTabSpec("albums").setIndicator("Albums", res.getDrawable(R.drawable.ic_tab_albums)) .setContent(R.id.textview2); tabHost.addTab(spec); intent = new Intent().setClass(this, SongsActivity.class); spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.ic_tab_songs)) .setContent(R.id.textview3); tabHost.addTab(spec); tabHost.setCurrentTab(2); } }

    2.Modify main.xml as follows

    <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:eek:rientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" >
    <TextView
    android:id="@+id/textview1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="this is the artists tab" />
    <TextView
    android:id="@+id/textview2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="this is the albums tab" />
    <TextView
    android:id="@+id/textview3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="this is the songs tab" />
    </FrameLayout>
    </LinearLayout> </TabHost>

    Hope you get the output correctly :)
     

Share This Page

Loading...