线程内的数据库访问给出空指针异常

时间:2013-04-13 06:28:48

标签: android sqlite thread-safety

I have a thread containing a socket connection ., 
irrespective of that within that i have  a db access .
Its giving null pointer exception . 
Why? 
is it restricted that db access within a thread? 
if i run the db access as a seperate method and 
if i call that outside the thread it works fine Any idea?
  

    @Override
public void run() {
    SharedPreferences prefs = 

PreferenceManager.getDefaultSharedPreferences(Filetransfer.this);   
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("Filename","");    
            editor.commit();

            System.out.println("insert over");
            filedb=openOrCreateDatabase(Table_Name,MODE_PRIVATE, null);
            t.setText("File: "+filenam);
            Calendar c = Calendar.getInstance(); 
            //setting time
            int seconds = c.get(Calendar.SECOND);
            int min= c.get(Calendar.MINUTE);
            int hour= c.get(Calendar.HOUR);
            String time=hour+":"+min+":"+seconds;
            //getting date
            SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
            String formattedDate = df.format(c.getTime());


            ContentValues values=new ContentValues();
            values.put("Time", time);
            values.put("Date", formattedDate);
            values.put("Filename", filenam);
            filedb.insert("Filedata", null, values);
            filedb.close();
    }

0 个答案:

没有答案