繁体   English   中英

当我将代码从一个应用程序移动到另一个应用程序时,用于编辑Google电子表格的Android应用程序崩溃

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

我对Java和Android开发都缺乏经验。 但是我设法制作了一个简单的Android应用程序来读取接近传感器值。 我还设法编写了一个Java应用程序,用于编辑Google电子表格中的单元格。

因此,现在我想将两者结合起来,并拥有Android应用程序,该应用程序将根据接近传感器的值更改google电子表格中单元格的值。

但是,当我合并代码时,我的应用程序崩溃了,并且我对Eclipse环境和调试器的了解不足,无法弄清发生了什么。 该应用程序将安装精美和午餐。 主视图仅显示一秒钟,然后应用崩溃。

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();
            }
            }
*/        
    }

}

当您尝试在UI中进行操作时,会发生这种情况。 您需要在后台执行(线程)。 您可以使用类似AsyncTask的工具:( http://developer.android.com/reference/android/os/AsyncTask.html )。

一些代码例如:

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.
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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