2013-03-29 19 views
1

我對Java和Android開發都很缺乏經驗。但我設法制作了一個簡單的Android應用程序來讀取接近傳感器值。我還設法編寫了一個在Google電子表格中編輯單元格的Java應用程序。當我將代碼從一個應用程序移動到另一個應用程序時,Android應用程序編輯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 protected]", "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(); 
      } 
      } 
*/   
    } 

} 

回答

0

這發生在你試圖在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. 
    } 
} 
相關問題