80,360
社区成员
发帖
与我相关
我的任务
分享
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);//这个地方提示空指针异常
}
}
<?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>
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
<?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>
<?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>