I want to change background color of selected view but after putting below code I got Error.
When user selected other view of gallery previous view come to default color but now I got below error
When I change selected item on gallery view error comes
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@SuppressLint("ResourceAsColor")
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// if(position != save)
// parent.getChildAt(position).setBackgroundResource(R.drawable.state_selected);
view.setBackgroundColor(Color.GREEN);
if (save != -1) {
parent.getChildAt(save).setBackgroundColor(Color.RED);
}
save = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Log
java.lang.NullPointerException
at com.example.csglobal.MainActivity$2.onItemSelected(MainActivity.java:119)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
at android.widget.AdapterView.selectionChanged(AdapterView.java:879)
at android.widget.Gallery.selectionChanged(Gallery.java:546)
at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1043)
at android.widget.Gallery.setSelectionToCenterChild(Gallery.java:592)
at android.widget.Gallery.trackMotionScroll(Gallery.java:396)
at android.widget.Gallery$FlingRunnable.run(Gallery.java:1504)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)