Android Activity with Listview keeps throwing NullPointerException

Go To StackoverFlow.com

0

I'm trying to create a ListView that displays 2 TextViews per row.

However I keep getting a NullPointerException when opening the Activity and I have no idea why (logCat logs don't tell me which line(s) on my end create the problem).

Here's the relevant code:

The main class EventViewActivity

public class EventViewActivity extends ListActivity {
public String[] eventTitles;

@Override
public void onCreate(Bundle savedInstanceState) {
    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

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

    eventTitles = new String[]{                 
            getResources().getString(R.string.eventtitle1),
            getResources().getString(R.string.eventtitle2),
            getResources().getString(R.string.eventtitle3),
            getResources().getString(R.string.eventtitle4),
            getResources().getString(R.string.eventtitle5)
    };

    setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_title, eventTitles));
    setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_descr, eventTitles));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Toast.makeText(getApplicationContext(), "You have clicked an item", Toast.LENGTH_SHORT).show();
}
}

layout.xml is:

 <LinearLayout xmlns:android="schemas.android.com/apk/res/android"
       android:layout_width="fill_parent" android:layout_height="wrap_content"
       android:orientation="vertical" >
       <ListView android:id="@+id/android:eventlist" android:layout_width="fill_parent"
        android:layout_height="fill_parent"
       android:background="@drawable/eventview_background"/>
</LinearLayout>

Thanks!

2012-04-04 06:28
by Mats Raemen
Post the logcat and also your eventview_layout.xmlAdil Soomro 2012-04-04 06:29
yeah for some reason when i add the eventviewlayout and the eventviewrow, i can't post the question because not all code is allegedly indented righ - Mats Raemen 2012-04-04 06:30
the eventview layout:

Mats Raemen 2012-04-04 06:32

the eventview_row:

Mats Raemen 2012-04-04 06:33

can you post your logcat??? that'll help debug - Mayank 2012-04-04 06:38
the error was using R.layout.eventviewlayout insteadof R.layout.eventviewrow in my ArrayAdapter. However, it seems I can use a Listadapter only once? It sets the last ListAdapter, but it doesn't set the first. However when I comment the last, it does set the first - Mats Raemen 2012-04-04 11:01


0

does your eventview_layout has a ListView with id android:id="@android:id/list" ..? if not add it.. i assume its not done

a small correction..

    <LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 
    <ListView android:id="@android:id/list"
 android:layout_width="fill_parent" 
android:layout_height="fill_parent"
 android:background="@drawable/eventview_background"/>
 </LinearLayout>

its necessary that the contentView of ListACtivity has a listView with exact same Id..

2012-04-04 06:32
by ngesh


0

I think the way you are approaching for ListView is not the correct.

Please have A look on this sample LINK to have complete view how create a ListView with custom adapter.

The above link has sample application created with listview and each listitem having two text fields as your requirement.

2012-04-04 06:54
by Shankar Agarwal


0

You should call the:

super.onCreate();

first!

2012-04-04 08:22
by TacB0sS
Ads