0

我正在使用ActionBarSherlock並創建了一個滑動選項卡顯示。我試圖通過使用對象引用來創建視圖,並且我已經通過使用父活動的名稱並使用它的轉換嘗試了它。我在我的結構中從較低級的課程中收到的活動中有對象引用。我希望能夠在每個片段中使用這些引用,但目前我所說的是對象爲空。有人可以看看我的代碼,看看我需要做些什麼來解決這個問題。如果您不完全瞭解我想說明的內容,請問我有關它的任何問題。提前致謝。在Android中使用ActionBarSherlock將對象從父項傳遞給片段

這裏是我的父活動代碼:

public class SlidingTabsActivity extends SherlockFragmentActivity 
{ 
ViewPager viewPager; 
TabsAdapter tabsAdapter; 
ActionBar actionBarTabs; 
Spinner spinner; 
ImageButton newFile; 
String[] languages = { "English", "Chinese", "German" }; 
ArrayAdapter<String> arrayAdapter; 
LayoutInflater spinnerLayoutInflater; 
View spinnerView; 

PopupFirmware popupFirmware; // Popup firmware class instance 
CommsLayerInterface commsLayerInterface; 
DeviceLayerInterface deviceLayerInterface; 

private DeviceInfo deviceInfo; 
private Pages pages; 
private Page page1; 
private Page page2; 
private Page page3; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    viewPager = new ViewPager(this); 
    viewPager.setId(R.id.pager); 
    setContentView(viewPager); 

    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, languages); 
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinnerLayoutInflater = (LayoutInflater) this.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    spinnerView = spinnerLayoutInflater.inflate(R.layout.spinner, null); 
    spinner = (Spinner) spinnerView.findViewById(R.id.tabsSpinner); 
    spinner.setAdapter(arrayAdapter); 
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
    { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) 
     { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) 
     { 
      // TODO Auto-generated method stub 

     } 

    }); 
    newFile = (ImageButton) spinnerView.findViewById(R.id.tabsImageButton); 
    newFile.setOnClickListener(new OnClickListener() 
    { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 

    }); 

    actionBarTabs = getSupportActionBar(); 
    actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    actionBarTabs.setCustomView(spinnerView); 
    actionBarTabs.setDisplayShowCustomEnabled(true); 
    actionBarTabs.setDisplayHomeAsUpEnabled(true); 

    tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view 

    popupFirmware = new PopupFirmware(this); // Declaring popup firmware class 

    commsLayerInterface = new CommsLayerInterface(); 
    deviceLayerInterface = new DeviceLayerInterface(this); 
    deviceLayerInterface.setCommsLayerInterface(commsLayerInterface); 
    deviceLayerInterface.updateValues(); 

    deviceInfo = deviceLayerInterface.getDeviceInfo(); 
    pages = deviceLayerInterface.getPages(); 
    setUpPages(pages); 

    /* Adds fragments to the tabs adapter */ 
    tabsAdapter.addTab(actionBarTabs.newTab().setText("PV"), Fragment_1.class, null); 
    tabsAdapter.addTab(actionBarTabs.newTab().setText("CONFIG"), Fragment_2.class, null); 
    tabsAdapter.addTab(actionBarTabs.newTab().setText("DIAG"), Fragment_3.class, null); 

} 

public DeviceInfo getDeviceInfo() 
{ 
    return deviceInfo; 
} 

public Pages getPages() 
{ 
    return pages; 
} 

public Page getPage1() 
{ 
    return page1; 
} 

public Page getPage2() 
{ 
    return page2; 
} 

public Page getPage3() 
{ 
    return page3; 
} 

@Override 
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) 
{ 
    com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater(); 
    inflater.inflate(R.menu.tabs, menu); 
    return super.onCreateOptionsMenu(menu);  
} 

@Override 
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) 
{ 
    switch (item.getItemId()) 
    { 
     case android.R.id.home: 
      /* Goes up a level in the application structure to the activity before this one */ 
      Intent upIntent = new Intent(this, MainActivity.class); 
      upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(upIntent); 
      return true; 
     case R.id.action_home: 
      /* Opens up the MainActivity activity */ 
      Intent homeIntent = new Intent(this, MainActivity.class); 
      startActivity(homeIntent); 
      return true; 
     case R.id.refresh: 
      /* Refreshes the current activity */ 
      Intent refreshIntent = getIntent(); 
      finish(); 
      startActivity(refreshIntent); 
      return true; 
     case R.id.action_settings: 
      /* Opens up the SettingsActivity activity */ 
      Intent settingsIntent = new Intent(this, SettingsActivity.class); 
      startActivity(settingsIntent); 
      return true; 
     case R.id.help: 
      /* Opens up the HelpActivity activity */ 
      Intent helpIntent = new Intent(this, HelpActivity.class); 
      startActivity(helpIntent); 
      return true; 
     case R.id.firmware_update: 
      /* Creates the popup dialog for the firmware options */ 
      popupFirmware.createDialog(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 

    } 

} 

private void setUpPages(Pages refPages) 
{ 
    Pages pagess = refPages; 

    ArrayList<Page> arrayListPage = pagess.getPages(); 

    for(Page p : arrayListPage) 
    { 
     if(p == arrayListPage.get(0)) 
     { 
      p = page1; 
     } 

     if(p == arrayListPage.get(1)) 
     { 
      p = page2; 
     } 

     if(p == arrayListPage.get(2)) 
     { 
      p = page3; 
     } 
    } 
} 

}

這裏是片段代碼:

public class Fragment_1 extends SherlockFragment 
{ 
private View view; 
private PVConfiguration pvConfiguration; 
private SlidingTabsActivity slidingTabsActivity; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    view = inflater.inflate(R.layout.fragment_pv, container, false); 

    slidingTabsActivity = (SlidingTabsActivity) getSherlockActivity(); 

    return view; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) 
{ 
    /* Creates the new views from the configuration class */ 
    pvConfiguration = new PVConfiguration(getSherlockActivity()); 
    pvConfiguration.setDeviceInfo(slidingTabsActivity.getDeviceInfo()); 
    pvConfiguration.setPage(slidingTabsActivity.getPage1()); 
    pvConfiguration.createView(); 
    super.onActivityCreated(savedInstanceState); 
} 
} 

回答

0

使用工廠方法來創建你的片段,並通過當你初始化它。 也不要忘記保存它onSaveStateInstace已經通過使物體Parceable

public class Fragment_1 extends SherlockFragment 
    { 
     private View view; 
     private PVConfiguration pvConfiguration; 
     private SlidingTabsActivity slidingTabsActivity; 

     public static Fragment_1 newInstance(PVConfiguration pvConfiguration){ 
      Fragment_1 frag = new Fragment_1(); 
      frag.pvConfiguration = pvConfiguration; 
      return frag; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     { 
      view = inflater.inflate(R.layout.fragment_pv, container, false); 

      slidingTabsActivity = (SlidingTabsActivity) getSherlockActivity(); 

      return view; 
     } 

     @Override 
     public void onActivityCreated(Bundle savedInstanceState) 
     { 
    /* Creates the new views from the configuration class */ 
      pvConfiguration = new PVConfiguration(getSherlockActivity()); 
      pvConfiguration.setDeviceInfo(slidingTabsActivity.getDeviceInfo()); 
      pvConfiguration.setPage(slidingTabsActivity.getPage1()); 
      pvConfiguration.createView(); 
      super.onActivityCreated(savedInstanceState); 
     } 
    } 
+0

我想從我的活動類通過對象的第一頁「和「DeviceInfo」拿回來onCreate。你的答案是否仍適用於此?或者你認爲我傳遞了'PVConfiguration'對象?謝謝您的回答。 –

+0

通過這種方式,您可以通過工廠方法傳遞各種對象,原始類型和接口。 – wrecker

+0

我加了方法,但沒有奏效。我在哪裏調用靜態newInstance方法? –

相關問題