我正在使用ActionBarSherlock并创建了一个滑动选项卡显示。我试图通过使用对象引用创建视图,我已经尝试使用父活动的名称并使用它的强制转换。我在活动中有对象引用,这是我在结构中从较低级别的类中获得的。我希望能够在每个片段中使用这些引用,但我现在所说的对象是null。有人可以看看我的代码,看看我需要做些什么来解决这个问题。如果你不完全理解我想要解释的内容,请问我有关它的任何问题。提前谢谢。
这是我的父活动代码:
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 :(得分:0)
使用工厂方法创建片段,并在初始化时传递它。
另外,请不要忘记保存onSaveStateInstace
通过制作对象onCreate
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);
}
}