我正在开发一个Android应用程序,该应用程序要求我定义主LinearLayouts中包含的某些LinearLayout,这些布局数据是通过run_time动态获取的,但是Container Layout是在XML中静态声明的,我添加了动态创建的布局到主布局,但是我得到了NullPointerException

02-13 11:03:45.745: WARN/System.err(620): java.lang.NullPointerException
02-13 11:03:45.745: WARN/System.err(620):     at com.example.solaceap.Media.initiatePopupWindow(Media.java:476)
02-13 11:03:45.755: WARN/System.err(620):     at android.view.View.performClick(View.java:3480)
02-13 11:03:45.765: WARN/System.err(620):     at android.view.View$PerformClick.run(View.java:13983)
02-13 11:03:45.765: WARN/System.err(620):     at android.os.Handler.handleCallback(Handler.java:605)
02-13 11:03:45.765: WARN/System.err(620):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-13 11:03:45.775: WARN/System.err(620):     at android.os.Looper.loop(Looper.java:137)
02-13 11:03:45.785: WARN/System.err(620):     at android.app.ActivityThread.main(ActivityThread.java:4340)
02-13 11:03:45.785: WARN/System.err(620):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 11:03:45.795: WARN/System.err(620):     at java.lang.reflect.Method.invoke(Method.java:511)
02-13 11:03:45.795: WARN/System.err(620):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-13 11:03:45.805: WARN/System.err(620):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-13 11:03:45.805: WARN/System.err(620):     at dalvik.system.NativeStart.main(Native Method)


这是我的代码:

主要布局:

 <LinearLayout
            android:id="@+id/music_popup"
            android:layout_width="300dp"
            android:layout_height="500dp"
            android:background="@drawable/ac_overlay"
            android:gravity="top"
            android:orientation="vertical"
            android:paddingBottom="12dp"
            android:paddingTop="12dp" >
</LinearLayout>


这是动态声明的布局:

   private View layout = null;
   LinearLayout music_pop_up = (LinearLayout) findViewById(R.id.music_popup);
   private void initiatePopupWindow(String section) {
                    layout = inflater.inflate(R.layout.music_popup,
                            (ViewGroup) findViewById(R.id.music_pop));

                    // create a 300px width and 470px height PopupWindow
                    pw = new PopupWindow(layout,
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT, true);
                    // display the popup in the center
                    pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

                    music_cancel = (Button) layout.findViewById(R.id.back_music);
                    music_cancel.setOnClickListener(onCancelClick);

                    get_music_albums();
    }
    public void get_music_albums(){
        int albums_num = albums_arr.size();
            for(int i = 0; i < albums_num; i++){
                LinearLayout music_layout = new LinearLayout(this);
                music_layout.setOrientation(LinearLayout.HORIZONTAL);
                music_layout.setLayoutParams(music_lp);
                music_pop_up.addView(music_layout); // this is where i got the Exception

                ImageButton poster = new ImageButton(this);
                poster.setTag(i);
                poster.setLayoutParams(album_lp);
                music_layout.addView(poster);
                poster.setClickable(true);
                poster.setAdjustViewBounds(true);
                poster.setScaleType(ScaleType.CENTER_INSIDE);
                poster.setBackgroundColor(Color.TRANSPARENT);
                poster.setImageResource(R.drawable.media_poster);

                TextView album_name = new TextView(this);
                album_name.setLayoutParams(album_name_lp);
                music_layout.addView(album_name);
                album_name.setTextSize(13);
                album_name.setTextColor(Color.WHITE);
                album_name.setText("test");

                TextView artist_name = new TextView(this);
                artist_name.setLayoutParams(artist_name_lp);
                music_layout.addView(artist_name);
                artist_name.setTextSize(8);
                artist_name.setTextColor(Color.CYAN);
                artist_name.setText("test");
            }
        }
    }


这就是我声明布局参数的方式:

        // Declaring the Music LayoutParams
        music_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 50);
        music_lp.gravity = Gravity.LEFT;
        music_lp.setMargins(20, 0, 0, 0);

        // Declaring the Album LayoutParams
        album_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
        album_lp.gravity = Gravity.CENTER;
        album_lp.setMargins(3, 3, 3, 3);

        // Declaring the Album_Name LayoutParams
        album_name_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        album_name_lp.gravity = Gravity.CENTER;

        // Declaring the Artist_Name LayoutParams
        artist_name_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        artist_name_lp.gravity = Gravity.BOTTOM;

最佳答案

好像您没有正确构建视图,
initiatePopupWindow(Media.java:476)在第476行崩溃
如果您不知道线性布局的大小,请考虑使用listview并在那里回收视图。

如果您不能使用listview,请在此行调试您的类,并检查为什么它仍然为空。

10-08 15:38