Search code examples
javaandroidlistviewandroid-activityandroid-listview

Why do I get an error in this ListView creation?


I'm trying to develop an ListView in Android that contains on Customer Name & Address and this list view is in a TabWidget. However, whenever I click that tab to see customer list info, application breaks. Following is my code:

The exception would be:

exception IllegalStateException (id=830019169944)
cause IllegalStateException (id=830019169944) detailMessage "Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?" (id=830019169976) stackState (id=830019170216)
stackTrace null suppressedExceptions ArrayList (id=830019170192)

XML page

<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:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TextView
                android:tag="tab0"
                android:text="MainMenu"
                android:background="@android:drawable/btn_star_big_on"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                />
            <TextView
                android:tag="tab1"
                android:text="Customers"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                />

        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/lblmainmenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Menu"
        android:textSize="20sp" />

    <ImageButton
        android:id="@+id/btnStock"
        android:layout_marginTop="20dp"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignLeft="@+id/lblmainmenu"
        android:layout_below="@+id/lblmainmenu"
        android:onClick="btn_clickstock"
        android:scaleType="fitCenter"
        android:src="@drawable/dailystock" />

    <ImageButton
        android:id="@+id/btnDailySummury"
        android:layout_marginTop="20dp"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginLeft="40dp"
        android:layout_below="@+id/lblmainmenu"
        android:layout_toRightOf="@+id/btnStock"
        android:onClick="btn_clickDailySummury"
        android:scaleType="fitCenter"
        android:src="@drawable/dailystock" />

</RelativeLayout>
<ListView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    </ListView>
</FrameLayout>
</LinearLayout>
</TabHost>

JAVA Main Activity code

public class MainMenuActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);

        Intent intent = new Intent(this, Customer_List_Activity.class);

        TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();

        final TabWidget tabWidget = tabHost.getTabWidget();
        final FrameLayout tabContent = tabHost.getTabContentView();

        // Get the original tab textviews and remove them from the viewgroup.
        TextView[] originalTextViews = new TextView[tabWidget.getTabCount()];
        for (int index = 0; index < tabWidget.getTabCount(); index++) {
            originalTextViews[index] = (TextView) tabWidget.getChildTabViewAt(index);
        }
        tabWidget.removeAllViews();

        // Ensure that all tab content childs are not visible at startup.
        for (int index = 0; index < tabContent.getChildCount(); index++) {
            tabContent.getChildAt(index).setVisibility(View.GONE);
        }

        // Create the tabspec based on the textview childs in the xml file.
        // Or create simple tabspec instances in any other way...
        for (int index = 0; index < originalTextViews.length; index++) {
            final TextView tabWidgetTextView = originalTextViews[index];
            final View tabContentView = tabContent.getChildAt(index);
            TabSpec tabSpec = tabHost.newTabSpec((String) tabWidgetTextView.getTag());
            if (index == 0)
            {
            tabSpec.setContent(new TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    return tabContentView;
                }
            });
            }
            else
                tabSpec.setContent(intent);
            if (tabWidgetTextView.getBackground() == null) {
                tabSpec.setIndicator(tabWidgetTextView.getText());
            } else {
                tabSpec.setIndicator(tabWidgetTextView.getText(), tabWidgetTextView.getBackground());
            }
            tabHost.addTab(tabSpec);


        }       


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

ListView Activity Code XML Page

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">

  <TextView android:id="@+id/text1"
    android:textSize="14dp"
    android:textColor="#FFFFFF"
    android:textStyle="italic"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

  <TextView android:id="@+id/text2"
    android:textSize="12dp"
    android:textColor="#BFFF00"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

ListView Activity Code Java

public class Customer_List_Activity extends ListActivity {
  private SimpleAdapter sa;
  ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    HashMap<String,String> item;
    for(int i=0;i<HardwareShops.length;i++){
      item = new HashMap<String,String>();
      item.put( "line1", HardwareShops[i][0]);
      item.put( "line2", HardwareShops[i][1]);
      list.add( item );
    }
    sa = new SimpleAdapter(this, list,
      R.layout.list_for_customer ,
      new String[] { "line1","line2" },
      new int[] {android.R.id.text1, android.R.id.text2});
    setListAdapter( sa );
  }

    private String[][] HardwareShops =

          {{"Jayasekara","Colombo 03"},

           {"Chandana","Colombo 03"},

           {"Ruban","Borella"},

           {"Safras","Colombo 05"},

           {"Harris","Rajagiriya"},

           {"HJ","Nawala"},

           {"Himali","Nugegoda"},

           {"Nilawala","Colombo 13"},

           {"Jayantha","Colombo 09"}};
}

What would be the problem? Is there any missing code line?


Solution

  • you need to change MainMenuActivity's base class from Activity to ActivityGroup, as follows:

      public class MainMenuActivity extends ActivityGroup {
      ...
      }
    

    ActivityGroup will take care of an instance of LocalActivityManager. So you don't need to create it. After the base class is changed, just call getLocalActivityManager() function defined in the base class to get that instance. Call tabHost's setup function like this:

     tabHost.setup(this.getLocalActivityManager());