简体   繁体   中英

Android app to edit google spread sheet crashes when I moved code from one application to another

I'm very inexperienced with both Java and Android development. But I managed to make a simple Android application to read the proximity sensor value. I also managed to write a Java application that edited a cell in a google spread sheet.

So now I want to combine the two and have the Android app that will change the value of a cell in a google spreadsheet based on the value of the poximity sensor.

However, when I combine the code, my app crashes and I don't know enough about the Eclipse environment and debuggers to figure out what's going on. The app installs fine and lunches. The main view shows up for just a second then the app crashes.

package com.example.mysensorproject;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.data.spreadsheet.CellFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class MainActivity extends Activity implements SensorEventListener {

        TextView proxText;
        SensorManager sm;
        Sensor proxSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sm=(SensorManager)getSystemService(SENSOR_SERVICE);
        proxSensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        proxText=(TextView)findViewById(R.id.proximityTextView);        
        sm.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @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 onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        proxText.setText(String.valueOf(event.values[0]));

        //change value in spreadsheet
        SpreadsheetService service = new SpreadsheetService("wise");
        service.setProtocolVersion(SpreadsheetService.Versions.V3);

        try {

            service.setUserCredentials("email@example.com", "password");
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }

        URL url = null;         
        try {
            url = new URL("https://spreadsheets.google.com/feeds/cells/0AuDoKcCfLURKdEQ3bVFLemY5aVh2SGQySE10QTYzRGc/od6/private/full");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        CellFeed cellFeed = null;
        try {
            cellFeed = service.getFeed(url, CellFeed.class);
//          System.out.println("Cell Feed Worked");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for (CellEntry cell : cellFeed.getEntries()) {
               cell.changeInputValueLocal(String.valueOf(event.values[0]));
               try {
                cell.update();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ServiceException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
*/        
    }

}

This occurs when u trying to do in the UI. You need to do in background (Thread). You can use something like AsyncTask: ( http://developer.android.com/reference/android/os/AsyncTask.html ).

Some code For example:

public class TaskModifySpreadsheet extends AsyncTask <Spreadsheet, Object, Spreadsheet> 
{
    @Override
    protected Spreadsheet doInBackground(Spreadsheet... titles) {
        // Do all work with the spreadsheet here. Modify cells, retrieving... etc.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM