程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> android-求大神指導Android中重寫SimpleAdapter類引getCount空指針異常,萬分感謝

android-求大神指導Android中重寫SimpleAdapter類引getCount空指針異常,萬分感謝

編輯:編程解疑
求大神指導Android中重寫SimpleAdapter類引getCount空指針異常,萬分感謝

重寫的BaseAdapter:

public class MyAdapter extends BaseAdapter {
    List<UserInfo> userInfos=new ArrayList<UserInfo>();
    Context context;

    public MyAdapter(Context c,List<UserInfo> userInfos) {
        // TODO Auto-generated constructor stub
        this.context=c;
        this.userInfos=userInfos;
    }

    public void setcotext(List<UserInfo> userInfos){
        this.userInfos=userInfos;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return userInfos.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return userInfos.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return userInfos.get(arg0).getId();
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        if(arg1==null){
            arg1=LayoutInflater.from(context).inflate(R.layout.book_item, null);
        }
        TextView _context=(TextView) arg1.findViewById(R.id.content);
        TextView _datetime=(TextView) arg1.findViewById(R.id.datetime);

        UserInfo u=userInfos.get(arg0);

        _context.setText(u.getContent());
        _datetime.setText(u.getDatetime());
        return arg1;
    }

}

Activity的:

 public class MainActivity extends Activity implements OnClickListener{
    Button add;
    Button re;
    SQLiteDatabase db;
    ListView listView;
    MyAdapter adapter;
    List<UserInfo> liUse;
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        shuaxin();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DbInit();
        add=(Button) findViewById(R.id.add);
        listView=(ListView) findViewById(R.id.lv);
        registerForContextMenu(listView);
        adapter=new MyAdapter(this , liUse);
        listView.setAdapter(adapter);
    }

    public List<UserInfo> selectAll(){
        List<UserInfo> ls=new ArrayList<UserInfo>();

        Cursor cur=db.rawQuery(" select * from user ", null);
        while (cur.moveToNext()) {
            UserInfo u = new UserInfo();
            u.setId(cur.getInt(cur.getColumnIndex("_id")));
            u.setContent(cur.getString(cur.getColumnIndex("content")));
            u.setDatetime(cur.getString(cur.getColumnIndex("datetime")));
            ls.add(u);
        }

        return liUse;

    }

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


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
        case R.id.refresh:
            shuaxin();
            break;

        case R.id.add:
            Intent intent=new Intent(this,AddConActivity.class);
            startActivity(intent);
            break;

        default:
            break;
        }

    }
    public void DbInit(){
        DBHelper dbHelper=new DBHelper(this, "Cotext.db", null, 1);
        db=dbHelper.getWritableDatabase();

    }
    class DBHelper extends SQLiteOpenHelper{

        public DBHelper(Context context, String name, CursorFactory factory,
                int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase arg0) {
            // TODO Auto-generated method stub
            SimpleDateFormat    formatter    =   new    SimpleDateFormat    ("yyyy年MM月dd日    HH:mm:ss     ");       
            Date    curDate    =   new    Date(System.currentTimeMillis());//獲取當前時間       
            String    str    =    formatter.format(curDate);       
            arg0.execSQL("create table user ( _id integer primary key  autoincrement , datetime text not null , content varchar(100))");
            ContentValues cv=new ContentValues();
            cv.put("datetime", str);
            cv.put("content", "測試");
            arg0.insert("user", "datetime", cv);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }

    }
    private void shuaxin() {
        liUse=selectAll();
        adapter.setcotext(liUse);
        adapter.notifyDataSetInvalidated();
    }
}

get 和 set :

 public class UserInfo implements Serializable{
    private int id;
    private String datetime;
    private String content;

    public UserInfo() {
        // TODO Auto-generated constructor stub
    }

    public UserInfo(int id,String datetime,String content) {
        // TODO Auto-generated constructor stub
        this.setId(id);
        this.setContent(content);
        this.setDatetime(datetime);

    }

    public UserInfo(String datetime,String content) {
        // TODO Auto-generated constructor stub
        this.setContent(content);
        this.setDatetime(datetime);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDatetime() {
        return datetime;
    }

    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


}

異常

04-23 16:48:45.585: E/AndroidRuntime(1823): FATAL EXCEPTION: main
04-23 16:48:45.585: E/AndroidRuntime(1823): Process: com.example.notepad, PID: 1823
04-23 16:48:45.585: E/AndroidRuntime(1823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notepad/com.example.notepad.MainActivity}: java.lang.NullPointerException
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.os.Handler.dispatchMessage(Handler.java:102)
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.os.Looper.loop(Looper.java:136)
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.app.ActivityThread.main(ActivityThread.java:5045)
04-23 16:48:45.585: E/AndroidRuntime(1823): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 16:48:45.585: E/AndroidRuntime(1823): at java.lang.reflect.Method.invoke(Method.java:515)
04-23 16:48:45.585: E/AndroidRuntime(1823): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-23 16:48:45.585: E/AndroidRuntime(1823): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-23 16:48:45.585: E/AndroidRuntime(1823): at dalvik.system.NativeStart.main(Native Method)
04-23 16:48:45.585: E/AndroidRuntime(1823): Caused by: java.lang.NullPointerException
04-23 16:48:45.585: E/AndroidRuntime(1823): at com.example.notepad.MyAdapter.getCount(MyAdapter.java:29)
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.widget.ListView.setAdapter(ListView.java:480)
04-23 16:48:45.585: E/AndroidRuntime(1823): at com.example.notepad.MainActivity.onCreate(MainActivity.java:44)
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.app.Activity.performCreate(Activity.java:5231)
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-23 16:48:45.585: E/AndroidRuntime(1823): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
04-23 16:48:45.585: E/AndroidRuntime(1823): ... 11 more


最佳回答:


04-23 16:48:45.585: E/AndroidRuntime(1823): at android.widget.ListView.setAdapter(ListView.java:480)
List liUse; liUse沒有初始化,是空指針吧,所以後面 adapter 中getCount 是崩潰了。
開始直接初始化就不會崩潰了,
List liUse = new ArrayList<>();

u010811339
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved