【问题标题】:Android Accelerometer SensorAndroid 加速度计传感器
【发布时间】:2011-07-07 09:19:55
【问题描述】:

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

它完美地工作。 但是当我将 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 public

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); }

}

感谢您的帮助。

【问题讨论】:

  • 是空指针异常吗?您似乎没有初始化变量 CONTEXT,如果您从某个地方调用静态方法 getContext() 肯定会导致错误。如果您发布 logcat 输出会很有帮助,这样我们就可以看到您遇到的错误。

标签: android accelerometer sensors


【解决方案1】:

对于 CONTEXT 尝试将其初始化为

this.getApplicationContext()

【讨论】:

  • 解决方案:this.getApplicationContext()
【解决方案2】:

上面的代码在 CONTEXT 的情况下有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); }

}

【讨论】:

  • 你已经在类中实现了 AccelerometerListsner 接口。我假设您已经编写了正确的代码,正如您所说的那样,当使用 Activity 扩展时,代码可以正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 2014-04-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-09
相关资源
最近更新 更多