astralmaster
astralmaster

Reputation: 2465

Multiple layout files Android

I have two XML files for my layout - main.xml and test.xml. Here is the code in test.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/menu_objoptions"   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent"  
        android:padding="10dp"  >  
    </ListView>  

</LinearLayout>

When I try to get an instance of the ListView with

findViewById( R.id.menu_objoptions )

it returns null. Why is that?

EDIT: here is the relevant java code:

public void objClick(String objid, long X, long Y) {

        final String objID = objid;
        final long x = X;
        final long y = Y;

        try {
            mHandle.runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    PopupWindow popUp;
                    LinearLayout layout;

                    ListView mainListView;  
                    ArrayAdapter<String> listAdapter;  
                    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",  
                            "Jupiter", "Saturn", "Uranus", "Neptune"};    

                    ArrayList<String> planetList = new ArrayList<String>();  
                    planetList.addAll( Arrays.asList(planets) );  

                    listAdapter = new ArrayAdapter<String>(mHandle, R.layout.menu_objoptions_row, planetList);  

                    listAdapter.add( "Ceres" );  
                    listAdapter.add( "Pluto" );  
                    listAdapter.add( "Haumea" );  
                    listAdapter.add( "Makemake" );  
                    listAdapter.add( "Eris" );  

                    popUp   = new PopupWindow(mHandle);
                    layout  = new LinearLayout(mHandle);

                    layout.setOrientation(LinearLayout.VERTICAL);

                    mainListView = (ListView) mHandle.findViewById( R.id.menu_objoptions );  
                    mainListView.setAdapter( listAdapter );     
                    layout.addView(mainListView);

                    popUp.setContentView(layout);
                    popUp.setOutsideTouchable(true);
                    popUp.showAtLocation(rlmain, Gravity.NO_GRAVITY, 10, 10);
                    popUp.update((int)x,(int)y, 300, 80);

                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }



    }

NullPointerException is thrown at this line:

mainListView.setAdapter( listAdapter );

Upvotes: 0

Views: 309

Answers (2)

Simon Dorociak
Simon Dorociak

Reputation: 33495

it returns null. Why is that?

Probably you calling it too early. You need to call it after

setContentView(R.layout.someLayout);

is called. setContentView method should be called before you start initialising another widgets, immediately after you call parent's constructor.

Update:

So in your case you need set your LinearLayout as contentView before you want to initialise any widget in your case ListView.

My suggestion is to initialise your LinearLayout in onCreate() method.

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.yourLinearLayout); // this must be called first
   ListView list = findViewById(R.id.menu_objoptions); // now it should works.
}

Upvotes: 2

vinoth
vinoth

Reputation: 225

You should initialize the LinearLayout after set the contentViewById (.....) in Oncreate() method.

The Code should be

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
               setContentView(R.layout.test);

        LinearLayout layout = (LinearLayout)findViewById(R.id.menu_objoptions);

Make sure that you are using LinearLayout reference for initialization.

Upvotes: 0

Related Questions