我有这个自定义微调器(有一个图标和一个数字),我试图在微调器上获取所选项的值。我正在使用片段,我似乎遇到了片段如何工作以及所有问题的一些问题。当我试图在微调器上调用NullPointerException
时,我得到setOnItemSelectedListener
。根据logcat错误,NPE的原因似乎来自AdapterView
。
AlertsFragment.java
public class AlertsFragment extends Fragment{
private Switch switch1;
int [] numbers={10,20,30,40,50,60,70, 80, 90, 100, 110, 120};
int flags[] = {R.drawable.low, R.drawable.low, R.drawable.low, R.drawable.low,
R.drawable.medium, R.drawable.medium, R.drawable.medium, R.drawable.medium,
R.drawable.high, R.drawable.high, R.drawable.high, R.drawable.high};
private AlertsFragment.OnFragmentInteractionListener listener;
public static AlertsFragment newInstance() {
return new AlertsFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_alerts, container, false);
switch1= (Switch)view.findViewById(R.id.LEDSwitch);
switch1.setChecked(false);
switch1.setTextOn("On");
switch1.setTextOff("Off");
Spinner spin = (Spinner)view.findViewById(R.id.LDRspinner);
CustomAdapter customAdapter=new CustomAdapter(getActivity(),flags,numbers);
spin.setAdapter(customAdapter);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
int value =Integer.valueOf(parent.getItemAtPosition(position).toString());
String alertMsg= "Alert Raised";
String alertMsg2= "No Alert Raised";
if(value< 50){
Toast.makeText(
getActivity(),alertMsg, Toast.LENGTH_LONG).show();
}
else if(value>50){
Toast.makeText(
getActivity(),alertMsg2, Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
@Override
public void onAttach(android.content.Context context) {
super.onAttach(context);
if (context instanceof AlertsFragment.OnFragmentInteractionListener) {
listener = (AlertsFragment.OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
public interface OnFragmentInteractionListener {
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
Context context;
int level[];
int[] numbers;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] level, int[] numbers) {
this.context = applicationContext;
this.level = level;
this.numbers = numbers;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return level.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_items, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(level[i]);
names.setText(String.valueOf(numbers[i]));
return view;
}
}
logcat的:
03-17 23:58:47.202 24238-24238/com.example.teerna.smartagriculturev5 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.teerna.smartagriculturev5.AlertsFragment$1.onItemSelected(AlertsFragment.java:60)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:899)
at android.widget.AdapterView.access$200(AdapterView.java:50)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:863)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
第60行AlertsFragment.java中的logcat错误指向:
int value =Integer.valueOf(parent.getItemAtPosition(position).toString());
我已经查看了与此相关的其他问题,我尝试了多种方法来解决此问题,方法是将CustomAdapter.java中的Context
更改为Activity
,或者将变量Context
初始化为null在AlertsFragment.java中的onAttach
方法中使用它但它们没有用。
我可能做错了,但我会感激一些帮助。
答案 0 :(得分:1)
您的CustomAdapter始终为所有位置返回null:
@Override
public Object getItem(int i) {
return null;
}
将其更改为:
@Override
public Object getItem(int i) {
return level[i];
}
可能会导致空指针异常