0
我要分配一些的OnClickListener
動態聲明存在於一個動態聲明LinearLayout
元素,我爲了對付這些存在於LinearLayout
的元素,但使用getChildAt()
我OnClickListener
內這樣做,我的應用程序崩潰,當我試圖刪除myChildAt()
就讓它涉及與LinearLayout
直接它工作得很好,這是我的代碼時:的Android應用程序崩潰,而該分配onClickListener
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
「Android應用程序崩潰」暗示,你應該運行'亞行logcat'並張貼在這裏的堆棧跟蹤的相關部分。 – keyboardsurfer
代碼在哪裏,你附加的,LinearLayout到ParentView –
'adb logcat'包括「由......引起」,如果有的話 – 18446744073709551615