为什么我在ListView创建中遇到错误?

时间:2014-09-21 06:45:49

标签: java android listview android-activity android-listview

我正在尝试在 Android 中开发 ListView ,其中包含客户名称&地址和此列表视图位于 TabWidget ...中!但是,每当我单击该选项卡查看客户列表信息时,应用程序就会中断。以下是我的代码......!

异常是 java.lang.IllegalStateException:您是否忘记调用' public void setup(LocalActivityManager activityGroup)'?

  

异常IllegalStateException(id = 830019169944)
    导致IllegalStateException(id = 830019169944)detailMessage"没有   你忘记调用公共无效设置(LocalActivityManager   的ActivityGroup)'" (id = 830019169976)stackState(id = 830019170216)
    stackTrace null suppressExceptions 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主要活动代码

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活动代码XML页面

<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活动代码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"}};
}

会出现什么问题?有没有丢失的代码行?请帮助我谢谢

1 个答案:

答案 0 :(得分:1)

您需要将MainMenuActivity的基类从Activity更改为ActivityGroup,如下所示:

  public class MainMenuActivity extends ActivityGroup {
  ...
  }

ActivityGroup将处理LocalActivityManager的一个实例。所以你不需要创建它。更改基类后,只需调用基类中定义的getLocalActivityManager()函数即可获取该实例。调用tabHost的设置功能如下:

 tabHost.setup(this.getLocalActivityManager());