【发布时间】:2016-03-09 04:47:31
【问题描述】:
我想更改所选视图的背景颜色,但在输入以下代码后出现错误。
当用户选择画廊的其他视图时,以前的视图变为默认颜色,但现在出现以下错误
当我在图库视图中更改所选项目时出现错误
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
}
});
日志
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)
【问题讨论】:
-
指出引发异常的确切行
-
请参考try this link这个链接
-
@YogeshBorhade:已经尝试过..同样的错误
-
发生这种情况是因为适配器中的 getView 方法更改回默认图像。您需要更改适配器的getview方法中的imge,然后我不会更改回默认图像。
标签: android android-layout android-intent