我想为动态声明的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
答案 0 :(得分:0)
最简单的解决方案是将侦听器直接设置为视图而不是覆盖父节点。
我不确定我是否得到了正确的视图,但您应该调用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);
}
});
}