MRefaat
MRefaat

Reputation: 535

NullPointerException when adding some LinearLayouts

I'm developing an Android app which requires me to define some LinearLayouts contained inside a main LinearLayout, these layouts data are dynamically obtained through the run_time, but the Container Layout is statically declared in the XML, I add the dynamically created Layouts to the main Layout but i got a 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)

and This is my code :

main Layout:

 <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>

And this is the dynamically declared layouts:

   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");
            }
        }
    }

And this is how i declare the layout params:

        // 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;

Upvotes: 0

Views: 454

Answers (3)

Lal
Lal

Reputation: 14810

Try using LayoutInflater

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

and use convertView to declare your elements

Upvotes: 0

M D
M D

Reputation: 47807

First try this you should change this

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

With

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

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

Upvotes: 1

Udi Oshi
Udi Oshi

Reputation: 6867

Seems like you're not building the views correctly, initiatePopupWindow(Media.java:476) crashes in line 476 If you don't know the size of linear layout consider using listview and recycle your views in there.

If you can't use listview debug your class in this line and check why it's still null.

Upvotes: 2

Related Questions