Category Archives: android-fragments

Error While Trying To save Location in database from googleAPIs function [duplicate]

This question already has an answer here:

if (googleMap != null) {


            googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

                @Override
                public void onMyLocationChange(Location location) {
                    // TODO Auto-generated method stub


                    double latitude = location.getLatitude();
                    double longitude = location.getLongitude();

                    String longi = String.valueOf(longitude);
                    String lati = String.valueOf(latitude);

                    SharedPreferences sharedPref = getSharedPreferences("userId", Context.MODE_PRIVATE);

                    String ID = sharedPref.getString("KEY_ID", " dupa");
                        googleMap.getMyLocation();

                        UserFunctions userFunction = new UserFunctions();
                        userFunction.setLocation(ID, longi, lati);

                    mTV.setText(ID + ",  " + longi + ", " + lati);


                }
            });

        }`

This code returns this error :

08-28 21:13:45.672 6236-6236/? E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.gcator, PID: 6236 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) at java.net.InetAddress.lookupHostByName(InetAddress.java:418) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) at java.net.InetAddress.getAllByName(InetAddress.java:215) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142) at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169) at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470) at com.gcator.library.JSONParser.getJSONFromUrl(JSONParser.java:41) at com.gcator.library.UserFunctions.setLocation(UserFunctions.java:64) at com.gcator.Map$1.onMyLocationChange(Map.java:110) at com.google.android.gms.maps.GoogleMap$2.zzq(Unknown Source) at com.google.android.gms.maps.internal.zzp$zza.onTransact(Unknown Source) at android.os.Binder.transact(Binder.java:380) at com.google.android.gms.maps.internal.bh.a(SourceFile:82) at com.google.maps.api.android.lib6.d.aj.a(Unknown Source) at com.google.maps.api.android.lib6.d.aj.a(Unknown Source) at com.google.maps.api.android.lib6.d.v.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

when i'll delete

UserFunctions userFunction = new UserFunctions(); 
userFunction.setLocation(ID, longi, lati);

everything works fine. I dont know whats the problem. setLocation should work fine cause its based on very similar functions. Please help :) (P.S. its my 1st fast question dont be harsh on me :D)

Read excel content and display it in table format in android application

I am trying to read the excel content and display it in the form of table in android application.I tried all the possible ways which I got from google but none of them did work for me. I tried using apache poi as well as jxl but both did not work. 

It would be great help for me if you can suggest me the code or steps.

For the below code I got /C:\Users\\Admin\Pictures\Book1.xls file not found error . / trailing slash added before the path.

                    Vector cellVectorHolder = new Vector();
            FileInputStream myInput = new 
                FileInputStream("C:\\Users\\Admin\\Pictures\\Book1.xls");
                    POIFSFileSystem myFileSystem = new
                POIFSFileSystem(myInput);   
            HSSFWorkbook myWorkBook = new
                HSSFWorkbook(myFileSystem);                 
            HSSFSheet mySheet = myWorkBook.getSheetAt(0);
                        Iterator rowIter = mySheet.rowIterator();
            while(rowIter.hasNext())
            {
                HSSFRow myRow = (HSSFRow) rowIter.next();
                Iterator cellIter = myRow.cellIterator();
                Vector cellStoreVector=new Vector();
                while(cellIter.hasNext()){
                HSSFCell myCell = (HSSFCell) cellIter.next();
                cellStoreVector.addElement(myCell);
            }
            cellVectorHolder.addElement(cellStoreVector);
        }
                        for(int i=0;i<cellVectorHolder.size();i++)
            {   String data=(String) cellVectorHolder.get(i);   }}}
second way
    public class ExcelActivity extends Activity {

 private String LOG_TAG = "ReadExcelFromUrl";
 ArrayList<ShoppingCart> shoppingCartList;    

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_comparison);

            // Hashmap for ListView

        // Loading Albums JSON in Background Thread
        new ExcelURL().execute();

        // get listview
        //ListView lv = getListView();

 }


private void displayCart() {

  //Array list of countries
  List<String> myList = new ArrayList<String>();
  for(int i = 0, l = shoppingCartList.size(); i < l; i++){
   ShoppingCart shoppingCart = shoppingCartList.get(i);
   String myData = shoppingCart.getItemNumber() + ": " +
       shoppingCart.getDescription() + "\nPrice: $" +
       shoppingCart.getPrice() + "\nQuantity: " +
       shoppingCart.getQuantity();
   myList.add(myData);
  }



 }


 private class ExcelURL extends AsyncTask<String, Void, String> {
  private static final int REGISTRATION_TIMEOUT = 3 * 1000;
  private static final int WAIT_TIMEOUT = 30 * 1000;
  private final HttpClient httpclient = new DefaultHttpClient();
  final HttpParams params = httpclient.getParams();
  HttpResponse response;
  private String content =  null;
  private ProgressDialog dialog = new ProgressDialog(ExcelActivity.this);

  protected void onPreExecute() {
   dialog.setMessage("Getting your data... Please wait...");
   dialog.show();
  }

  protected String doInBackground(String... urls) {

   String URL = null;

   BufferedReader br=null;
 StringBuffer sb=new StringBuffer();
       try {
        parseExcel( getAssets().open(
                        "book1.xls"));

        } catch (ClientProtocolException e) {
            Log.w("HTTP2:",e );
            content = e.getMessage();
            cancel(true);
           } catch (IOException e) {
            Log.w("HTTP3:",e );
            content = e.getMessage();
            cancel(true);
           }catch (Exception e) {
            Log.w("HTTP4:",e );
            content = e.getMessage();
            cancel(true);
           }

           return content;


  }

  protected void onCancelled() {
   dialog.dismiss();
   Toast toast = Toast.makeText(ExcelActivity.this, 
     "Error connecting to Server", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.TOP, 25, 400);
   toast.show();

  }

  protected void onPostExecute(String content) {
   dialog.dismiss();
   displayCart();
   //Display the Excel data in a ListView
    // updating UI from Background Thread
    runOnUiThread(new Runnable() {
        public void run() {
            /**
             * Updating parsed JSON data into ListView
             * */
    /*      ListAdapter adapter = new SimpleAdapter(
                    ExcelActivity.this, shoppingCartList,
                    R.layout.list_item_comparision, new String[] { TAG_ID,
                            TAG_NAME,COMPLETE_NAME}, new int[] {
                            R.id.album_id, R.id.album_name,R.id.album_cname });

            // updating listview
            setListAdapter(adapter);*/
        }
    });
  }



  private void parseExcel(InputStream fis){

   shoppingCartList = new ArrayList<ShoppingCart>();    

   try{

    // Create a workbook using the Input Stream 
    HSSFWorkbook myWorkBook = new HSSFWorkbook(fis);

    // Get the first sheet from workbook 
    HSSFSheet mySheet = myWorkBook.getSheetAt(0);

    // We now need something to iterate through the cells
    Iterator<Row> rowIter = mySheet.rowIterator();
    while(rowIter.hasNext()){

     HSSFRow myRow = (HSSFRow) rowIter.next();
     // Skip the first 2 rows
     if(myRow.getRowNum() < 2) {
      continue;
     }

     ShoppingCart shoppingCart = new ShoppingCart();

     Iterator<Cell> cellIter = myRow.cellIterator();
     while(cellIter.hasNext()){

      HSSFCell myCell = (HSSFCell) cellIter.next();
      String cellValue = "";

      // Check for cell Type
      if(myCell.getCellType() == HSSFCell.CELL_TYPE_STRING){
       cellValue = myCell.getStringCellValue();
      }
      else {
       cellValue = String.valueOf(myCell.getNumericCellValue());
      }

      // Just some log information
      Log.v(LOG_TAG, cellValue);

      // Push the parsed data in the Java Object
      // Check for cell index
      switch (myCell.getColumnIndex()) {
      case 0: 
       shoppingCart.setItemNumber(cellValue);
       break;
      case 1: 
       shoppingCart.setDescription(cellValue);
       break;
      case 2: 
       shoppingCart.setPrice(Double.valueOf(cellValue));
       break;
      case 3: 
       shoppingCart.setQuantity(Double.valueOf(cellValue));
       break;
      default: 
       break;
      }

     }

     // Add object to list
     shoppingCartList.add(shoppingCart);
    }
   }
   catch (Exception e){
    e.printStackTrace(); 
   }

  }

 }
}

Could not find class 'java.awt.font.FontRenderContext', referenced from method org.apache.poi.hssf.usermodel.HSSFSheet.autoSizeColumn

Could not find class 'java.awt.font.FontRenderContext', referenced from method org.apache.poi.hssf.usermodel.HSSFSheet.autoSizeColumn Could not find class 'java.awt.font.FontRenderContext', referenced from method org.apache.poi.hssf.usermodel.HSSFSheet.autoSizeColumn

please suggest the issues