2011-07-07 86 views
0

我正在尝试使用加速计传感器。所以我试过这个 的例子: http://blog.androgames.net/85/android-accelerometer-tutorial/Android加速计传感器

它的工作很完美。 但是,当我将AccelerometerManager活动更改为服务时,它不起作用,并且出现错误。

//this is the activity that i want change 
public class Accelerometer extends Activity 
     implements AccelerometerListener { 

    private static Context CONTEXT; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     CONTEXT = this; 
    } 

    protected void onResume() { 
     super.onResume(); 
     if (AccelerometerManager.isSupported()) { 
      AccelerometerManager.startListening(this); 
     } 
    } 

    protected void onDestroy() { 
     super.onDestroy(); 
     if (AccelerometerManager.isListening()) { 
      AccelerometerManager.stopListening(); 
     } 

    } 

    public static Context getContext() { 
     return CONTEXT; 
    } 

    /** 
    * onShake callback 
    */ 
    public void onShake(float force) { 
     Toast.makeText(this, "Phone shaked : " + force, 1000).show(); 
    } 

    /** 
    * onAccelerationChanged callback 
    */ 
    public void onAccelerationChanged(float x, float y, float z) { 
     ((TextView) findViewById(R.id.x)).setText(String.valueOf(x)); 
     ((TextView) findViewById(R.id.y)).setText(String.valueOf(y)); 
     ((TextView) findViewById(R.id.z)).setText(String.valueOf(z)); 
    } 

} 

//这是我的服务时,我改变它,我的错误是HIR公共

class Accelerometer extends Service implements AccelerometerListener{ private static Context CONTEXT; 

@Override 
public IBinder onBind(Intent intent) { 
// TODO Put your code here 
return null; 
} 

@Override 
public void onCreate() { 
System.out.println(”start listening”); 
// if (AccelerometerManager.isSupported()) { AccelerometerManager.startListening(this); 

// } 
} 

@Override 
public void onDestroy() { 
System.out.println(”start listening”); 
// if (AccelerometerManager.isListening()) { AccelerometerManager.stopListening(); 
// } 
} 

public static Context getContext() { 
return CONTEXT; 
} 

/** 
* onShake callback 
*/ 
public void onShake(float force) { 
Toast.makeText(this, “Phone shaked niktilha omha ya 3ammi el7ag: ” + force, 1000).show(); } 

/** 
* onAccelerationChanged callback 
*/ 
public void onAccelerationChanged(float x, float y, float z) { System.out.println(”x = “+x+” y = “+y+” z = “+z); } 

} 

感谢您的帮助。

+0

是一个NullPointerException异常?你似乎没有初始化变量CONTEXT,并且如果你从某个地方调用了静态方法getContext(),肯定会导致错误。如果你发布logcat输出,这将有很大的帮助,所以我们可以看到你得到的错误。 – Marmoy

回答

0

上下文尝试初始化它作为

this.getApplicationContext() 
+0

解决方案:this.getApplicationContext() –

-1

上面的代码在上下文的情况下,有NULLPointerException。这就是为什么应用程序崩溃。虽然显示吐司完成使用这个。使用getApplicationContext()。希望这会解决你的问题。

修改代码:

class Accelerometer extends Service implements AccelerometerListener{ 

@Override 
public IBinder onBind(Intent intent) { 
// TODO Put your code here 
return null; 
} 

@Override 
public void onCreate() { 
System.out.println(”start listening”); 
// if (AccelerometerManager.isSupported()) { AccelerometerManager.startListening(this); 

// } 
} 

@Override 
public void onDestroy() { 
System.out.println(”stop listening”); 
// if (AccelerometerManager.isListening()) { AccelerometerManager.stopListening(); 
// } 
} 

/** 
* onShake callback 
*/ 
public void onShake(float force) { 
Toast.makeText(getApplicationContext(), “Phone shaked niktilha omha ya 3ammi el7ag: ” + String.valueOf(force), 1000).show(); } 

/** 
* onAccelerationChanged callback 
*/ 
public void onAccelerationChanged(float x, float y, float z) { System.out.println(”x = “+x+” y = “+y+” z = “+z); } 

} 
+0

您已经在clas中实现了AccelerometerListsner接口。我假设你已经写下了正确的代码,正如你所说的那样,在扩展Activity的情况下代码正在工作 –