2011-07-07 14 views
0

私はAccelerometer Sensorを使用しようとしています。だから私はこれを試した 例: http://blog.androgames.net/85/android-accelerometer-tutorial/Android Accelerometer Sensor

それは完全に動作します。 しかし、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

は、上記のコードは、CONTEXTの場合にNULLPointerExceptionを持っていた

this.getApplicationContext() 
+0

解決策:this.getApplicationContext() –

-1

としてそれを初期化してみてください。なぜアプリケーションがクラッシュしたのかトーストを行っている間にこれを使用してください。 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

クラス内にAccelerometerListsnerインターフェイスを実装しました。私はあなたが正しいコードを書いていると仮定しています。あなたがアクティビティで拡張された場合 –

関連する問題