【发布时间】:2015-05-16 08:04:14
【问题描述】:
我正在尝试使用自定义适配器实现微调器。但是 setAdapter 正在返回 NPE。我尝试过做各种各样的事情,包括通过堆栈溢出中的此类讨论帖子。请不要给我一个类似答案的链接。我当然已经完成了每一个,对我的代码进行了更改。
这是我的 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);
}
}
这是我的事件日志:
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)
【问题讨论】:
-
spinner_updated是什么?我的意思是spinner.setOnItemSelectedListener(spinner_updated);投掷 NPE。我想你错过了初始化spinner_updated接口。 -
不。我有它。我只是觉得没有必要在这里写。它什么也不做,只是返回所选项目的位置。
-
发布完整的活动代码。
-
我已经发布了我的整个活动,并更新了对 MyAdapter 的回调。请看一看。
-
在代码中标记第 44 行。
标签: java android android-spinner