Reputation: 79
I'm trying to assign content to a spinner in a fragment when the activity starts. I figured out that first I need to inflate the fragment, inflate the layout, and then inflate the view in order to assign it this piece of code:
spinner.setAdapter(adapter);
I've looked at other post on a question similar to this, and I'm not getting any luck. Ultimately, I'm not trying to fix my code, but rather find a piece of code that will allow me to to do spinner.setAdapter(adapter); without getting a NullPointerException. This action needs to happen when the activity starts, so basically it needs to involve the onCreate() method. Doing:
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.priority_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
putting this code inside the onCreate() will give me a NullPointerException because the spinner is inside the fragment and not the activity.
Here's the current code I am trying that doesn't work, but again, I'm looking for a solution to the problem not my code:
public class ActivityInfo extends ActionBarActivity {
Spinner b;
Myfragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_info);
Intent intent = getIntent();
String name = intent.getStringExtra("Name");
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new Myfragment();
fragmentTransaction.add(R.id.RelLayout, fragment);
fragmentTransaction.commit();
b = (Spinner) findViewById(R.id.spinner1);
fragment.addSpinner();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_info, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class Myfragment extends Fragment {
RelativeLayout relativeLayout;
View rootView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
relativeLayout = (RelativeLayout) rootView.findViewById(R.id.RelLayout);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_activity_info, container, false);
return rootView;
}
public void addSpinner()
{
Spinner spin = new Spinner(getActivity()); // needs activity context
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.priority_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
relativeLayout.addView(spin);
}
}
}
Upvotes: 0
Views: 1162
Reputation: 1494
Try doing this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_activity_info, null);
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.priority_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
return rl;
}
Upvotes: 2