加速度计应用程序编码有帮助吗?

发布于 2024-12-12 03:50:02 字数 3999 浏览 0 评论 0原文

我正在编写一个应用程序来显示 Android 中加速度计在 X、Y 和 Z 方向上的值。我是一名 Android 菜鸟,所以请耐心等待。这是我的代码,它非常简单,但我收到一个空指针异常,我不确定我的代码有什么问题。谢谢!

package com.*****.accelo;

import android.app.Activity;

import android.content.Context;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.widget.TextView;



public class AcceloActivity extends Activity implements SensorEventListener{



TextView textViewX, textViewY, textViewZ, textViewError;

StringBuilder builderx = new StringBuilder();
StringBuilder buildery = new StringBuilder();
StringBuilder builderz = new StringBuilder();




public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);

    textViewError = (TextView) findViewById(R.id.textViewError);
    textViewX = (TextView) findViewById(R.id.textViewX);
    textViewY = (TextView) findViewById(R.id.textViewY);
    textViewX = (TextView) findViewById(R.id.textViewZ);




    SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    Sensor accelerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        if(!manager.registerListener(this, accelerometer,  
SensorManager.SENSOR_DELAY_UI)){
        setContentView(textViewError);

        }
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {



}



public void onSensorChanged(SensorEvent event) {

    builderx.setLength(0);
    buildery.setLength(0);
    builderz.setLength(0);

    builderx.append("X " + event.values[0]);
    buildery.append("Y"+  event.values[1] );
    builderz.append("Z " + event.values[2]);

    textViewX.setText(builderx.toString());
    textViewY.setText(buildery.toString());
    textViewZ.setText(builderz.toString());

}



}

来自 logcat:

10-24 19:52:43.574: ERROR/AndroidRuntime(711): java.lang.RuntimeException: Unable to   
start activity ComponentInfo{com.amrit.accelo/com.amrit.accelo.AcceloActivity}: 
java.lang.NullPointerException
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at  
android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at     
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at     
android.os.Handler.dispatchMessage(Handler.java:99)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at 
android.os.Looper.loop(Looper.java:123)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
android.app.ActivityThread.main(ActivityThread.java:3683)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at      
java.lang.reflect.Method.invokeNative(Native Method)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at    
java.lang.reflect.Method.invoke(Method.java:507)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
dalvik.system.NativeStart.main(Native Method)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): Caused by: java.lang.NullPointerException      
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
com.amrit.accelo.AcceloActivity.onCreate(AcceloActivity.java:50)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at     
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

谢谢!

I'm writing an app to display the values of the accelerometer in Android in the X, Y, and Z directions. I am an Android noob so please bear with me. Here is my code, it is quite simple but I receive a null pointer exception and I'm not sure what's wrong with my code. Thanks!

package com.*****.accelo;

import android.app.Activity;

import android.content.Context;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.widget.TextView;



public class AcceloActivity extends Activity implements SensorEventListener{



TextView textViewX, textViewY, textViewZ, textViewError;

StringBuilder builderx = new StringBuilder();
StringBuilder buildery = new StringBuilder();
StringBuilder builderz = new StringBuilder();




public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);

    textViewError = (TextView) findViewById(R.id.textViewError);
    textViewX = (TextView) findViewById(R.id.textViewX);
    textViewY = (TextView) findViewById(R.id.textViewY);
    textViewX = (TextView) findViewById(R.id.textViewZ);




    SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    Sensor accelerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        if(!manager.registerListener(this, accelerometer,  
SensorManager.SENSOR_DELAY_UI)){
        setContentView(textViewError);

        }
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {



}



public void onSensorChanged(SensorEvent event) {

    builderx.setLength(0);
    buildery.setLength(0);
    builderz.setLength(0);

    builderx.append("X " + event.values[0]);
    buildery.append("Y"+  event.values[1] );
    builderz.append("Z " + event.values[2]);

    textViewX.setText(builderx.toString());
    textViewY.setText(buildery.toString());
    textViewZ.setText(builderz.toString());

}



}

From logcat:

10-24 19:52:43.574: ERROR/AndroidRuntime(711): java.lang.RuntimeException: Unable to   
start activity ComponentInfo{com.amrit.accelo/com.amrit.accelo.AcceloActivity}: 
java.lang.NullPointerException
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at  
android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at     
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at     
android.os.Handler.dispatchMessage(Handler.java:99)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at 
android.os.Looper.loop(Looper.java:123)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
android.app.ActivityThread.main(ActivityThread.java:3683)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at      
java.lang.reflect.Method.invokeNative(Native Method)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at    
java.lang.reflect.Method.invoke(Method.java:507)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
dalvik.system.NativeStart.main(Native Method)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): Caused by: java.lang.NullPointerException      
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
com.amrit.accelo.AcceloActivity.onCreate(AcceloActivity.java:50)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at     
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-24 19:52:43.574: ERROR/AndroidRuntime(711):     at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怕倦 2024-12-19 03:50:02

您从第 50 行收到此错误:

textViewError.setText("Error,无法注册传感器监听器");

因此您的textViewError变量在这里为空。您需要创建一个 XML 布局,使用 setContentView,然后将 TextView 添加到 XML 布局中,然后使用 findViewById 将 TextView 分配给它。 这可能是一个很好的起点

编辑:

为了清楚起见,您需要在调用 findViewById 之前设置内容视图。

public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);

    setContentView(R.layout.yourlayout);

    textViewError = (TextView) findViewById(R.id.textViewError);
    textViewX = (TextView) findViewById(R.id.textViewX);
    textViewY = (TextView) findViewById(R.id.textViewY);
    textViewX = (TextView) findViewById(R.id.textViewZ);
    ...

这里重要的是,您必须在 res/layout/ 下有一个名为 yourlayout.xml 的 xml 布局文件,其中包含您正在加载的视图。

You are getting this error from line 50:

textViewError.setText("Error, could not register sensor listener");

so your textViewError variable is null here. You will need to create an XML layout, use setContentView, and then add a TextView to your XML layout and then assign your TextView to it with findViewById. This is probably a good place to start.

EDIT:

Just to be clear, you need to set content view before you call findViewById.

public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);

    setContentView(R.layout.yourlayout);

    textViewError = (TextView) findViewById(R.id.textViewError);
    textViewX = (TextView) findViewById(R.id.textViewX);
    textViewY = (TextView) findViewById(R.id.textViewY);
    textViewX = (TextView) findViewById(R.id.textViewZ);
    ...

Important here is that you must have an xml layout file called yourlayout.xml under res/layout/ which contains the views that you are loading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文