Android 在listview上显示静态数据

Android 在listview上显示静态数据,android,listview,nullpointerexception,listadapter,Android,Listview,Nullpointerexception,Listadapter,我想在android中显示listview上的一些数据,我正在使用setlistadapter,但是当我在oncreate中获得列表“getlist”时,它会给我空指针异常 请在这方面帮助我 谢谢 以下是我的源代码: public class WOWActivity extends ListActivity { ListView listView ; @Override protected void onCreate(Bundle savedInstanceState

我想在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;
    }

}
公共类WOWAActivity扩展了ListActivity{
列表视图列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_-wow);
String[]newsFeed=getResources().getStringArray(R.array.Feeds);
listView=getListView();
this.setListAdapter(新的ArrayAdapter(this,R.layout.list_wow,R.id.url,newsFeed));
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.wow,menu);
返回true;
}
}
活动内容:

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

列表(w):

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

苹果对桔子

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


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

您无需从ListActivity扩展类,只需获取listView作为:

listView = (ListView)findViewById(R.id.list);
接下来将适配器设置为ListView

listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_wow ,R.id.url, newsFeed));
listView.setAdapter(新的ArrayAdapter(this,R.layout.list_wow,R.id.url,newsFeed));
按此进行更改

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

检查这个,我尝试了这篇文章,但是我在ListView=(ListView)findViewById(R.id.list)处给出了错误;我在xml中添加了列表,但仍然无法从xml中获取id。请从TextView android中删除此列表:visibility=“gone”@Shazar Wel-come。很高兴帮助你!我需要你的帮助我正在尝试实现GCM谷歌云消息,基本上我有一个通过移动服务器通信分配任务的应用程序,每当有人分配任务时,安卓设备会收到弹出式通知,你能在这方面帮我吗?
 android:visibility="gone"