Android空指针异常问题,新手求教

IT_163 2014-06-02 01:10:11
package com.stu.sqlite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.stu.bean.Student;
import com.stu.service.StudentService;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
private StudentService studentService=null;
private ListView listView=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
studentService=new StudentService(getApplicationContext());

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

show();
}

private void show() {
List<Student> students=studentService.getScrollData(0,20);

List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();

for(Student student:students)
{
Map<String,Object> map=new HashMap<String, Object>();
map.put("name",student.getName());
map.put("age",student.getAge());
map.put("money",student.getMoney());
map.put("id",student.getId());
data.add(map);
}

//创建SimpleAdapter对象
SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(), data,R.layout.item,
new String[]{"name","age","money"},new int[]{R.id.name,R.id.age,R.id.money});

listView.setAdapter(adapter);//这个地方提示空指针异常

}
}



...全文
233 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhbd1989 2014-06-03
  • 打赏
  • 举报
回复
引用 4 楼 hjywyj 的回复:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/listView" />
 
</LinearLayout>
好萌的头像
IT_163 2014-06-02
  • 打赏
  • 举报
回复
呵呵,问题解决了,谢谢楼上大神,唉,这么低级的错误都犯
fireyou 2014-06-02
  • 打赏
  • 举报
回复
android:text="@+id/listView" 这里应该是 android:id="@+id/listView"
  • 打赏
  • 举报
回复
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/listView" />
 
</LinearLayout>
韩曙亮 2014-06-02
  • 打赏
  • 举报
回复
IT_163 2014-06-02
  • 打赏
  • 举报
回复
出错异常


06-02 01:02:05.599: E/AndroidRuntime(17060): FATAL EXCEPTION: main
06-02 01:02:05.599: E/AndroidRuntime(17060): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stu.sqlite/com.stu.sqlite.MainActivity}: java.lang.NullPointerException
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at android.os.Looper.loop(Looper.java:123)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at android.app.ActivityThread.main(ActivityThread.java:4627)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at java.lang.reflect.Method.invokeNative(Native Method)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at java.lang.reflect.Method.invoke(Method.java:521)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at dalvik.system.NativeStart.main(Native Method)
06-02 01:02:05.599: E/AndroidRuntime(17060): Caused by: java.lang.NullPointerException
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at com.stu.sqlite.MainActivity.show(MainActivity.java:49)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at com.stu.sqlite.MainActivity.onCreate(MainActivity.java:27)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-02 01:02:05.599: E/AndroidRuntime(17060): 	... 11 more


IT_163 2014-06-02
  • 打赏
  • 举报
回复
这是main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@+id/listView" />

</LinearLayout>
IT_163 2014-06-02
  • 打赏
  • 举报
回复
这是item.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView 
        android:layout_width="150px"
        android:layout_height="50px"
        android:gravity="center"
        android:id="@+id/name"/>

    <TextView 
        android:layout_width="150px"
        android:layout_height="50px"
         android:gravity="center"
        android:id="@+id/age"/>
    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="50px"
         android:gravity="center"
        android:id="@+id/money"/>
    	
</LinearLayout>

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧