我想为OnClickListener
存在于动态声明中的某些动态声明的元素分配一个LinearLayout
,我正在getChildAt()
内部使用OnClickListener
,以便处理存在于中的元素,LinearLayout
但是这样做时我的应用程序崩溃了,当我尝试删除myChildAt()
并只是让它LinearLayout
直接处理就可以了,这是我的代码:
List<LinearLayout> inner_ver = new ArrayList<LinearLayout>();
for(int i = 0 ; i < size ; i++){
LinearLayout temp_inner_ver = new LinearLayout(this);
temp_inner_ver.setLayoutParams(temp_lay);
temp_inner_ver.setOrientation(LinearLayout.VERTICAL);
temp_inner_ver.setWeightSum(2);
temp_inner_ver.setPadding(7, 7, 7, 7);
inner_ver.add(temp_inner_ver);
}
for(int j = 0 ; j < inner_ver.size() ; j++){
// the LinearLayout declaration
inner_ver.get(j).addView(icon1);
icon1.setLayoutParams(lp_icon);
icon1.setBackgroundResource(R.drawable.ac_overlay);
icon1.setOrientation(LinearLayout.HORIZONTAL);
icon1.setTag(NORMAL);
// the elements declaration
TextView text1 = new TextView(this);
icon1.addView(text1);
text1.setLayoutParams(text_name);
text1.setText("something");
text1.setTextSize(12);
text1.setTextColor(Color.WHITE);
ImageButton image = new ImageButton(this);
icon1.addView(image);
image.setLayoutParams(lp_ineer_ver);
image.setImageResource(R.drawable.grpbuttonfocus6);
image.setBackgroundColor(Color.TRANSPARENT);
Button testbut = new Button(this);
icon1.addView(testbut);
testbut.setLayoutParams(lp_ineer_ver);
testbut.setText(" 8");
testbut.setTextSize(12);
testbut.setBackgroundColor(Color.TRANSPARENT);
testbut.setTextColor(Color.WHITE);
ImageButton testcol = new ImageButton(this);
icon1.addView(testcol);
testcol.setLayoutParams(lp_ineer_ver);
testcol.setImageResource(R.drawable.home_cool);
testcol.setBackgroundColor(Color.TRANSPARENT);
TextView text2 = new TextView(this);
icon1.addView(text2);
text2.setLayoutParams(lp_ineer_ver);
text2.setText("00");
text2.setTextSize(12);
assignListener(icon1);
}
public void assignListener(final LinearLayout l) {
l.getChildAt(1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
l.getChildAt(1).setBackgroundResource(R.drawable.home_grpbuttonfocus10);// this Line Getting a NullPointerException
}
});
}
并使其如下所示时,它可以正常工作:
public void assignListener(final LinearLayout l) {
l.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
l.setBackgroundResource(R.drawable.home_grpbuttonfocus10);
}
});
}
LogCat视图:(它NullPointerException
在代码上方的行中给了我一个)
01-14 12:22:06.308: INFO/ActivityManager(93): START {flg=0x4000000 cmp=com.example.solaceap/.HomeView (has extras)} from pid 602
01-14 12:22:06.308: WARN/WindowManager(93): Failure taking screenshot for (222x135) to layer 21015
01-14 12:22:06.625: DEBUG/dalvikvm(602): GC_FOR_ALLOC freed 11K, 3% free 11649K/11975K, paused 86ms
01-14 12:22:06.995: DEBUG/AndroidRuntime(602): Shutting down VM
01-14 12:22:06.995: WARN/dalvikvm(602): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): FATAL EXCEPTION: main
01-14 12:22:07.047: ERROR/AndroidRuntime(602): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.solaceap/com.example.solaceap.HomeView}: java.lang.NullPointerException
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at android.os.Looper.loop(Looper.java:137)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at java.lang.reflect.Method.invoke(Method.java:511)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at dalvik.system.NativeStart.main(Native Method)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): Caused by: java.lang.NullPointerException
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at com.example.solaceap.HomeView.assignListener(HomeView.java:669)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at com.example.solaceap.HomeView.setRooms(HomeView.java:518)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at com.example.solaceap.HomeView.setAllRooms(HomeView.java:359)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at com.example.solaceap.HomeView.declare_dynamics(HomeView.java:307)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at com.example.solaceap.HomeView.declare(HomeView.java:62)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at com.example.solaceap.HomeView.onCreate(HomeView.java:45)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at android.app.Activity.performCreate(Activity.java:4465)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-14 12:22:07.047: ERROR/AndroidRuntime(602): ... 11 more
最简单的解决方案是将侦听器直接设置为视图,而不是遍历父级。
我不确定我是否获得正确的视图,但您应该致电assignListener(image);
(应该是icon1布局的子级1)。
比只使用assignListener()
您发布的第二个版本:
public void assignListener(final ImageView imageView) { // changed type
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.home_grpbuttonfocus10);
}
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句