2014-09-21 76 views
1

我試圖開發一種的ListView的Android上客戶名稱中包含&地址和該列表視圖是在TabWidget ...!但是,每當我點擊該標籤查看客戶列表信息時,應用程序就會中斷。以下是我的代碼...!爲什麼在此ListView創建中會出現錯誤?

例外情況是java.lang.IllegalStateException:你忘記調用'public void setup(LocalActivityManager activityGroup)'了嗎?

異常IllegalStateException異常(ID = 830019169944)
原因IllegalStateException異常(ID = 830019169944)detailMessage 「難道 你忘了叫 '公共無效設置(LocalActivityManager 的ActivityGroup)'?」 (ID = 830019169976)stackState(ID = 830019170216)
堆棧跟蹤空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主要活動代碼

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"}}; 
} 

會是什麼問題呢?有沒有缺少的代碼行?請幫我謝謝

+0

粘貼logcat錯誤! – 2014-09-21 06:49:40

+0

有兩個選項卡,第二個選項卡與意圖一起使用 – gayan1991 2014-09-21 06:53:24

回答

1

你需要從活動改變MainMenuActivity的基類的ActivityGroup,如下所示:

public class MainMenuActivity extends ActivityGroup { 
    ... 
    } 

的ActivityGroup將採取LocalActivityManager的一個實例的照顧。所以你不需要創建它。在更改基類之後,只需調用基類中定義的getLocalActivityManager()函數即可獲取該實例。調用tabHost的設置功能是這樣的:

tabHost.setup(this.getLocalActivityManager()); 
相關問題