1. 程式人生 > >新手android 開發 錯誤集錦(持續更新中)

新手android 開發 錯誤集錦(持續更新中)

(1)error opening trace file: No such file or directory (2)

這是寫的第一個程式就出現的問題,而且查詢程式碼沒有發現錯誤。google後得出結論:模擬器版本和android的API版本不對應,相應的進行修改就行。

(2)出現java.lang.NumberFormatException: unable to parse 'null' as integer

 

 出現問題查出錯誤出在上面程式碼中intent傳回來的值有可能是null,就會產生轉換的錯誤,最終修改方案是加入異常處理機制,就是使用try and catch。修改後程式執行正常。程式碼如下:

public class otheractivity extends Activity{

private TextView textview2 = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_other);

textview2 = (TextView)findViewById(R.id.TextView2);

int oneint = 0;

int twoint = 0; 

Intent intent2 = getIntent();

String onestr = intent2.getStringExtra("one");

String twostr = intent2.getStringExtra("two");

try{

oneint = Integer.parseInt(onestr);

}

catch(Exception e){}

try{

twoint = Integer.parseInt(twostr);

}

catch (Exception e) {

// TODO: handle exception

}

int res = oneint + twoint;

String resstr = String.valueOf(res);

textview2.setText(resstr);

}

}

問題就迎刃而解了

(3)用一個監聽器實現多個按鈕的onclick監聽。

示例程式碼如下: 

public class SensorActivity extends Activity {

private Button button1 = null;

private Button button2 = null;

private Button button3 = null;

private Button button4 = null;

private Button button5 = null;

private Button button6 = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_sensor);

button1 = (Button)findViewById(R.id.button1);

button2 = (Button)findViewById(R.id.button2);

button3 = (Button)findViewById(R.id.button3);

button4 = (Button)findViewById(R.id.button4);

button5 = (Button)findViewById(R.id.button5);

button6 = (Button)findViewById(R.id.button6);

button1.setOnClickListener(new MyButtonlistener());

button2.setOnClickListener(new MyButtonlistener());

button3.setOnClickListener(new MyButtonlistener());

button4.setOnClickListener(new MyButtonlistener());

button5.setOnClickListener(new MyButtonlistener());

button6.setOnClickListener(new MyButtonlistener());

}

class MyButtonlistener implements OnClickListener {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

int i[] = new int[6];

for(int j = 0;j<6;j++){

i[j]=j;

}

HashMap<Button,Integer> bf = new HashMap<Button, Integer>(6);

bf.put(button1, i[0]);

bf.put(button2, i[1]);

bf.put(button3, i[2]);

bf.put(button4, i[3]);

bf.put(button5, i[4]);

bf.put(button6, i[5]);

//Intent intent[] = new Intent[6];

Intent intent1 = new Intent();

Intent intent2 = new Intent();

Intent intent3 = new Intent();

Intent intent4 = new Intent();

Intent intent5 = new Intent();

Intent intent6 = new Intent();

//intent[0].setClass(SensorActivity.this, Sensor1.class);

//intent[1].setClass(SensorActivity.this, Sensor2.class);

//intent[2].setClass(SensorActivity.this, Sensor3.class);

//intent[3].setClass(SensorActivity.this, Sensor4.class);

//intent[4].setClass(SensorActivity.this, Sensor5.class);

//intent[5].setClass(SensorActivity.this, Sensor6.class);

intent1.setClass(SensorActivity.this, Sensor1.class);

intent2.setClass(SensorActivity.this, Sensor2.class);

intent3.setClass(SensorActivity.this, Sensor3.class);

intent4.setClass(SensorActivity.this, Sensor4.class);

intent5.setClass(SensorActivity.this, Sensor5.class);

intent6.setClass(SensorActivity.this, Sensor6.class);

Button button = (Button)v;

int num = bf.get(button);

switch (num) {

case 0:

SensorActivity.this.startActivity(intent1);

break;

case 1:

SensorActivity.this.startActivity(intent2);

break;

case 2:

SensorActivity.this.startActivity(intent3);

break;

case 3:

SensorActivity.this.startActivity(intent4);

break;

case 4:

SensorActivity.this.startActivity(intent5);

break;

case 5:

SensorActivity.this.startActivity(intent6);

break;

default:

break;

}

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// TODO Auto-generated method stub

menu.add(0, 1, 1, R.string.exit);

menu.add(0, 2, 2, R.string.about);

return super.onCreateOptionsMenu(menu);

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// TODO Auto-generated method stub

if(item.getItemId()==1){

finish();

}

return super.onOptionsItemSelected(item);

}

關鍵在於用hashmap將控制元件與整數對應,然後進行選擇。

 (4)在同一個activity中使用兩個或兩個以上的感測器。
在同一個activity中使用多個感測器時,只需要宣告一個sensormanager就可以了。但是每個感測器例項都需要單獨宣告。

監聽器可以只使用一個監聽器。

具體示例如下:(以兩個感測器為例)

private SensorManager sm_gyroscope;

private Sensor my_gyroscope;

private Sensor my_rotation;

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.sensor4)

sm_gyroscope = (SensorManager)getSystemService(SENSOR_SERVICE);

my_gyroscope = sm_gyroscope.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

my_rotation = sm_gyroscope.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

}

private class Mysensorlistener implements SensorEventListener{

@Override

public void onAccuracyChanged(Sensor sensor, int accuracy) {

// TODO Auto-generated method stub

}

int temp1 = 0,temp2 = 0;

@Override

public void onSensorChanged(SensorEvent event) {

// TODO Auto-generated method stub

if(event.sensor.getType() == Sensor.TYPE_GYROSCOPE){

         //此處新增程式碼

}

}

if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR){

//此處新增程式碼

}

}

}

}

Mysensorlistener mylistener = new Mysensorlistener();

@Override

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

sm_gyroscope.unregisterListener(mylistener);

}

@Override

protected void onResume() {

// TODO Auto-generated method stub

super.onResume();

sm_gyroscope.registerListener(mylistener, my_gyroscope, SensorManager.SENSOR_DELAY_NORMAL);

sm_gyroscope.registerListener(mylistener, my_rotation, SensorManager.SENSOR_DELAY_NORMAL);

}