我有一个片段和一个活动。该活动使片段膨胀。片段的 xml 文件包含微调器的 @+id。
在活动的 onCreate 中,我试图从片段中获取微调器。
Spinner mySpinner = (Spinner)findViewById(R.id.mySpinner);
mySpinner 始终以空值返回。我绝对确定身份证是正确的。
我该如何解决?
编辑:这是活动的完整 onCreate :
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newtimer);
myArray = this.getResources().getStringArray(R.array.myspinner);
db = new DBAdapter(this);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle type = getIntent().getExtras();
String stringType = type.getString("type");
if (stringType.equals(A_STRING_HERE))
{
MyCustomFragment myFragment = new MyCustomFragment();
fragmentTransaction.replace(R.id.fragmentContainer, myFragment); // fragmentContainer is a FrameLayout that I use to swap with fragments
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
//TODO Load active timers
db.open();
Cursor myCursor = db.getEntry(0);
if(myCursor.moveToFirst())
{
String curr = myCursor.getString(2);
Spinner mySpinner = (Spinner)findViewById(R.id.myspinner;
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.myspinner,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if (myspinnerr == null)
{
Log.d("NewTimerActivity", "spinner is null!");
}
myspinner.setAdapter(adapter);
int i = java.util.Arrays.asList(myArray).indexOf(curr);
myspinner.setSelection(i);
}
}