Question

I want to show some data on listview in android,I am using setlistadapter for this,but when I get the list "getlist" in oncreate it gives me null pointer exception,

pls help me in this regard Thanks

Here Is my source code:

public class WOWActivity extends ListActivity {

    ListView listView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wow);

        String[] newsFeed = getResources().getStringArray(R.array.Feeds);

        listView = getListView();

        this.setListAdapter( new ArrayAdapter<String>(this, R.layout.list_wow ,R.id.url, newsFeed));


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.wow, menu);
        return true;
    }

}

Activity_wow:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".WOWActivity" >

    <ListView
        android:id = "@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="false" >

        </ListView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

List_wow:

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

    <TextView android:id="@+id/url"
        android:visibility="gone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    </LinearLayout>

String Value:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="Feeds">
        <item >Apples versus Oranges </item>

    </string-array>

</resources>
Was it helpful?

Solution

Change by this.

public class WOWActivity extends ListActivity {

ListView listView ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listView = getListView();
    String[] newsFeed = getResources().getStringArray(R.array.Feeds);
    this.setListAdapter( new ArrayAdapter<String>(this, R.layout.list_wow ,R.id.url, newsFeed));
}

EDIT:

Remove this from TextView

 android:visibility="gone"

OTHER TIPS

just change the following in WOWActivity.java

change listView = getListView(); to listView = (ListView)findViewById(R.id.list);

You needn't extends your class from ListActivity, just get listView as:

listView = (ListView)findViewById(R.id.list);

Next set your adapter to ListView

listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_wow ,R.id.url, newsFeed));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top