添加一些LinearLayouts时出现NullPointerException

时间:2014-02-13 09:14:53

标签: java android android-layout layout nullpointerexception

我正在开发一个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;

3 个答案:

答案 0 :(得分:2)

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

如果你不能使用listview在这一行调试你的类,并检查为什么它仍然是null。

答案 1 :(得分:1)

首先尝试这个你应该改变这个

 layout = inflater.inflate(R.layout.music_popup,
                        (ViewGroup) findViewById(R.id.music_pop));

使用

 LayoutInflater layout ;
 layout = (LayoutInflater) getApplicationContext().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);

 View v = layout.inflate(R.layout.music_popup, null);

答案 2 :(得分:0)

尝试使用LayoutInflater

LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
    View convertView = (LinearLayout) inflater.inflate(R.layout.yourxmlfile, null);

并使用convertView声明您的元素