创建微调器时发现错误。似乎旋转器是空的,但我不确定问题出在哪里。我在strings.xml中创建了一个字符串数组。有没有我错过的东西?
码
public class NewWorkOrder extends ActionBarActivity {
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_work_order);
this.spinner = (Spinner) findViewById(R.id.workTypeSpinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.array.work_type, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_work_order, 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 PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_new_work_order,
container, false);
return rootView;
}
}
}
logcat的
06-11 03:02:30.174: E/AndroidRuntime(1389): FATAL EXCEPTION: main
06-11 03:02:30.174: E/AndroidRuntime(1389): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cosmic.workorder/com.cosmic.workorder.NewWorkOrder}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.cosmic.workorder:id/container) for fragment PlaceholderFragment{41735b50 #0 id=0x7f05003c}
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.os.Looper.loop(Looper.java:137)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-11 03:02:30.174: E/AndroidRuntime(1389): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 03:02:30.174: E/AndroidRuntime(1389): at java.lang.reflect.Method.invoke(Method.java:525)
06-11 03:02:30.174: E/AndroidRuntime(1389): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-11 03:02:30.174: E/AndroidRuntime(1389): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 03:02:30.174: E/AndroidRuntime(1389): at dalvik.system.NativeStart.main(Native Method)
06-11 03:02:30.174: E/AndroidRuntime(1389): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.cosmic.workorder:id/container) for fragment PlaceholderFragment{41735b50 #0 id=0x7f05003c}
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.app.Activity.performStart(Activity.java:5143)
06-11 03:02:30.174: E/AndroidRuntime(1389): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-11 03:02:30.174: E/AndroidRuntime(1389): ... 11 more
答案 0 :(得分:0)
使用它:(我已经测试过了)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,array);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
答案 1 :(得分:0)
更改
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.array.work_type, android.R.layout.simple_spinner_item);
到
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, R.array.work_type);
适配器将数组作为最后一个参数,但您将其作为第二个参数传递。将其作为第三个参数传递。它应该工作
答案 2 :(得分:0)
你的微调器可能在片段布局中,而不是活动...所以把你的工作放在onCreateView里面这样:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_new_work_order,
container, false);
Spinner spinner = (Spinner) rootView.findViewById(R.id.workTypeSpinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, R.array.work_type);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
return rootView;
}