在listview上显示静态数据

时间:2014-03-07 07:36:11

标签: android listview nullpointerexception listadapter

我想在android中的listview上显示一些数据,我正在使用setlistadapter,但是当我在oncreate中获取列表“getlist”时它会给我空指针异常,

在这方面请帮助我 感谢

这是我的源代码:

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>

字符串值:

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

    </string-array>

</resources>

3 个答案:

答案 0 :(得分:2)

由此改变。

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

编辑:

从TextView中删除它

 android:visibility="gone"

答案 1 :(得分:0)

只需在WOWActivity.java中更改以下内容

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

答案 2 :(得分:0)

您无需从ListActivity扩展您的类,只需将listView作为:

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

接下来将适配器设置为ListView

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