我正在尝试使用自定义适配器实现微调器。但是setAdapter正在返回NPE。我已经尝试过各种各样的事情,包括在Stack Overflow中讨论这样的讨论帖子。请不要给我一个类似答案的链接。我当然经历了每一个,改变了我的代码。
这是我的StartActivity:
public class StartActivity extends Activity{
int selected;
String[] strings = {"CoderzHeaven","Google",
"Microsoft", "Apple", "Yahoo","Samsung"};
String[] subs = {"Heaven of all working codes ","Google sub",
"Microsoft sub", "Apple sub", "Yahoo sub","Samsung sub"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new MyAdapter(getApplicationContext(), R.layout.row, strings);
// 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);
spinner.setOnItemSelectedListener(spinner_updated);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
public class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row= convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
}
TextView label=(TextView)row.findViewById(R.id.company);
label.setText(strings[position]);
TextView sub=(TextView)row.findViewById(R.id.sub);
sub.setText(subs[position]);
return row;
}
}
AdapterView.OnItemSelectedListener spinner_updated = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selected = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public void park_clicked(View view){
Intent myIntent = new Intent(StartActivity.this, MainActivity.class);
myIntent.putExtra("selected", selected); //Optional parameters
StartActivity.this.startActivity(myIntent);
}
}
这是我的eventLog:
05-16 13:06:16.373 9030-9030/parkpurdue.parkpurdue E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{parkpurdue.parkpurdue/parkpurdue.parkpurdue.StartActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2107)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2132)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4918)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at parkpurdue.parkpurdue.StartActivity.onCreate(StartActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5185)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
答案 0 :(得分:0)
您的布局 - activity_start
,不包含ID = spinner
的Spinner视图。