1

So I'm trying to make a tabbed application...But it keeps crashing with null pointer exception . I checked through all the variables that could be causing a null pointer and I think I've narrowed it down.

ListView activeList = (ListView) findViewById(R.id.activelist);
if(activeList == null) {
  Log.e("com.name.app", "activeList null");
}

This returns a null. Should it? I'm using fragments to try and build a tabbed layout. This is the xml it's referencing.

<?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
        android:id="@+id/activelist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:persistent="true" >    
    </ListView>

</LinearLayout>

Thank you for any help!

Edit:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs_layout);

That's what my contentview looks like.

Edit: This is what my final code looked like!

    LayoutInflater inflater = (LayoutInflater)   getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout mContainer = (LinearLayout)     inflater.inflate(R.layout.tab_frag1_layout, null); 
    ListView activeList = (ListView) mContainer.findViewById(R.id.activelist);
3
  • RU inflating the view or doing as setContentview()??? Commented Sep 7, 2012 at 8:19
  • Look if that listview is declared in the same layout which you are setting as a content of that activity. Commented Sep 7, 2012 at 8:22
  • It is not, but now I'm trying to figure out how to inflate that. Is there any easy way? Commented Sep 7, 2012 at 8:33

2 Answers 2

4
LinearLayout mContainer = inflater.inflate(R.layout.linear_layout, null); 
ListView activeList = (ListView) mContainer.findViewById(R.id.activelist);

Where R.layout.linear_layout is the id of LinearLayout which contains your ListView.

Sign up to request clarification or add additional context in comments.

5 Comments

By RelativeLayout do you mean LinearLayout? Would I have to add an ID to the LinearLayout I have posted above? What would the inflater variable look like?
I modified with linearlayour but fro this issue it will be the same. I'm not sure where exactly you want to "inflate" your list.
R.layout.linear_layout <-- Where's that supposed to be in my xml?
post your R.layout.tabs_layout please
Oh wait! I got it! I'll post my final code above. Thank you all for your help!
1

Did you reffer in your activity to your XML layout?

You can do this in your oncreate method:

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

2 Comments

Oh! No I have another ContentView selected, but that's necessary. But I believe I need to inflate the textview that I need to use?
does this work? ListView tv = (ListView ) getLayoutInflater().inflate(R.layout.yourLV, null);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.