我尝试在活动开始时将内容分配给片段中的微调器。我发现首先我需要给片段充气,给布局充气,然后给视图充气,以便为它分配这段代码:
spinner.setAdapter(adapter);
我已经查看了与此类似的问题的其他帖子,我没有得到任何运气。最终,我并没有尝试修复我的代码,而是找到一段允许我做spinner.setAdapter(adapter)的代码;没有得到NullPointerException。这个动作需要在活动开始时发生,所以基本上它需要涉及onCreate()方法。这样做的:
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);
将此代码放在onCreate()中会给我一个NullPointerException,因为微调器位于片段内而不是活动中。
这是我正在尝试的当前代码无法正常工作,但同样,我正在寻找问题的解决方案,而不是我的代码:
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);
}
}
}
答案 0 :(得分:1)
尝试这样做:
@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;
}