2013-07-29 36 views
1

我正在開發Android應用程序和應用程序有行動欄包含刷新按鈕和溢出菜單和下面有兩個堆棧選項卡的操作欄。當我點擊刷新按鈕單個選項卡將刷新該任務我想從外部類使用意圖調用內部類,所以我可以從服務器刷新內部類。

當我嘗試從外部類使用意圖調用內部類我遇到異常像ActivityNotfound異常,但我已經在清單解決後提到內部類。我嘗試了很多方法,但沒有奏效。
如何在Android中使用intent在外部類中調用內部類?

下面是我的階級結構

public class Fragment extends SherlockFragmentActivity { 
    ActionBar actionBar = getSupportActionBar(); 

    actionBar.setDisplayHomeAsUpEnabled(false); 

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    Tab tab = actionBar.newTab(). 
      setText("Tab1"). 
      setTabListener(new Tab1()) 
      ; 
    actionBar.addTab(tab); 

    tab = actionBar.newTab(). 
      setText("Tab2"). 
      setTabListener(new Tab2()) 
      ; 
    actionBar.addTab(tab); 

    //I am tried number ways to refesh the particlar tab but not achieve my goal 
    public void Refresh() { 

     /* I am also write Refresh method on individual tab and call from 
     onOptionsItemSelected but not working*/ 

     Intent href = new Intent(Fragment.this, 
       Tab1.class); 
     startActivity(href); 
     finish(); 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
     case R.id.menu_refresh: 
      Refresh(); 
      return true; 
     default: 
      break; 
     } 
    } 
    //inner class for tab first 
    public class Tab1 extends SherlockListFragment implements ActionBar.TabListener{ 

     // here implementating the TabListener method 

     //here I am loading the list from server 
     private class LoadProjects extends AsyncTask<String, String, String> { 
      //call webservice 
     } 
    } 

    // inner class for tab second 
    public class Tab2 extends SherlockListFragment implements ActionBar.TabListener{ 

     // here implementating the TabListener method 

     //here I am loading the list from server 

     private class LoadProjects extends AsyncTask<String, String, String> { 
      //call webservice 
     } 
    } 
} 

AndroidManifest.xml中

<application> 
     <activity 
      android:name="com.ojaswitech.bookingscape.Fragment" 
      android:configChanges="orientation|keyboardHidden" 
      android:label="" 
      android:logo="@drawable/action_bar_logo" 
      android:theme="@style/Theme.New_theme_bs" > 
     </activity> 
     <activity 
      android:name="com.ojaswitech.bookingscape.Fragment$Tab1" 
      android:configChanges="orientation|keyboardHidden" > 
     </activity> 
     <activity 
      android:name="com.ojaswitech.bookingscape.Fragment$Tab2" 
      android:configChanges="orientation|keyboardHidden" /> 
    </application> 

請人幫助我如何做上述任務。 在此先感謝

+1

幫助的TAB1類不是一個活動,而是一個片段。 startActivity方法僅適用於活動。 片段在FragmentManager的幫助下顯示。 – thaussma

+0

@Herrmann你有什麼解決方案嗎? –

回答

0

您應該使Tab1在新文件中的公共類,而不是內部類。這將解決Refresh()方法「Activity not found」異常。

+0

我已經做了類似的工作,如果你需要示例代碼,請讓我知道。 –

+0

是啊我想要示例代碼,所以我可以更改我的代碼。 –

+0

我有我的完整代碼發佈在這裏。請檢查它.http://stackoverflow.com/questions/17918198/how-to-keep-tabs-visible-on-every-activity/17918804#17918804希望它會有所幫助。 –

0

我可以問你爲什麼要在這裏使用Intents?我的意思是意圖是用來啓動或與活動進行交流的抽象/包裝。請閱讀Fragment概念,以更好地瞭解片段如何與宿主活動進行交流,反之亦然。

在這種情況下,您可能需要在您的子片段中實現updateUI或refreshUI方法。點擊刷新按鈕,獲取你的片段(通過id或實例)並調用孩子的更新/刷新方法。爲How to refresh fragment tab content on button click

快樂編碼

見的解決方案!

+0

這可能也有幫助http://stackoverflow.com/questions/11578000/android-how-to-restart-refresh-a-fragment-from-fragmentactivty – VenoM

+0

我不明白從第一個鏈接是我應該寫什麼刷新方法()。 –

+0

哦!抱歉。第一個鏈接是給你一個片段的開始,你應該知道什麼是基本的東西。 只是出於好奇,你試驗片段就像這是你第一次在你的應用程序中使用片段?根據你的回答,我可能會指出你到一個安全的地方。 – VenoM

0

因此您可以刷新片段與your_activity_context.recreate()

+0

對我來說正常工作.. !! Thanxs Andrain。 – 2013-09-07 12:18:07