SensorManager"线程启动"错误

时间:2014-04-15 15:34:56

标签: android sensormanager

我正在尝试创建一个使用加速计来检测用户移动并触发某些事件的应用。该应用程序需要大量的工作,但每次我在手机中运行应用程序时,我都会收到ERROR“线程启动”,但似乎没有任何结果。

这是LogCat:

    04-15 17:46:21.380: E/BANANA(2516): Before sensor
    04-15 17:46:21.380: D/SensorManager(2516): registerListener :: handle = 0  name= K2DM delay= 200000 Listener= com.example.rockpaperaccelerate.UseSensor@413cdf98
    04-15 17:46:21.380: E/SensorManager(2516): thread start
    04-15 17:46:21.520: E/BANANA(2516): AFter sensor
    04-15 17:46:21.680: D/libEGL(2516): loaded /system/lib/egl/libGLES_android.so
    04-15 17:46:21.800: D/libEGL(2516): loaded /system/lib/egl/libGLES_rhea.so
    04-15 17:46:21.960: D/BRCM_EGL(2516): eglCreateContext() config: 18 context: 0x12e0a0, VC context 1, Thread 2516
    04-15 17:46:21.960: D/BRCM_EGL(2516): eglCreateWindowSurface() surface: 0x2aa080, VC surface: 1, Thread: 2516
    04-15 17:46:21.960: D/BRCM_EGL(2516): eglMakeCurrent(0x12e0a0, 0x2aa080, 0x2aa080) Thread: 2516
    04-15 17:46:21.980: D/OpenGLRenderer(2516): Enabling debug mode 0
    04-15 17:46:22.441: D/CLIPBOARD(2516): Hide Clipboard dialog at Starting input: finished by someone else... !

这是一切发生的活动:

public class UseSensor extends Activity implements SensorEventListener{

    private SensorManager mSensorManager;
    private Sensor mSensor;
    public int choice;
    public WebView myWebView1;
    public WebView myWebView2;
    public TextView myTextView;
    public String louts[]={"rock","paper","scissors"};
    public int i=0;
    public float gravity[]={0,0,0}; 
    public float linear_acceleration[]={0,0,0};

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rock_paper_accelerate_with_sensors);
        Log.e("BANANA","Before sensor");

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);


        Log.e("BANANA","AFter sensor");


    }

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.radio1:
                if (checked)
                    // player chose rock
                    choice=1;

                break;              
            case R.id.radio2:
                if (checked)
                    // player chose paper
                    choice=2;

                break;
            case R.id.radio3:
                if (checked)
                    // player chose scissors
                    choice=3;

                break;  
        }



    }

public void checkWin(int choice){

        Random generator = new Random();
        int machine = generator.nextInt( 3 )+1;
        setContentView(R.layout.show_results);
        myWebView1 = (WebView) findViewById(R.id.webView1);
        myWebView2 = (WebView) findViewById(R.id.webView2);
        myTextView = (TextView) findViewById(R.id.textView1);
        switch (machine){
        case 1: myWebView1.loadUrl("file:///android_asset/1.jpg");
        break;
        case 2: myWebView1.loadUrl("file:///android_asset/2.jpg");
        break;
        case 3: myWebView1.loadUrl("file:///android_asset/3.jpg");
        break;
        }

        switch (choice){
        case 1: myWebView2.loadUrl("file:///android_asset/1.jpg");
        break;
        case 2: myWebView2.loadUrl("file:///android_asset/2.jpg");
        break;
        case 3: myWebView2.loadUrl("file:///android_asset/3.jpg");
        break;
        }

        if (choice==machine){
            myTextView.setText("This is a tie!");
        }
        else if ((choice==3 && machine==2)||(choice==2 && machine==1)||(choice==1 && machine==3)){
            myTextView.setText("You Win!");
        }
        else{
            myTextView.setText("You Lose!");
        }
        Button myButton =  (Button)findViewById(R.id.button1);
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                i=0;
                Intent intent= new Intent(v.getContext(), UseSensor.class);
                startActivityForResult(intent, 0);

            }
        });


    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub

    }
    @Override

    public void onSensorChanged(SensorEvent event) {


        final float alpha = (float) 0.8;

        gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
        gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
        gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

        linear_acceleration[0] = event.values[0] - gravity[0];
        linear_acceleration[1] = event.values[1] - gravity[1];
        linear_acceleration[2] = event.values[2] - gravity[2];

          if (linear_acceleration[1]<-4.0){
             if(i<3){
                 int findId = this.getResources().getIdentifier("com.example.rockpaperaccelerate:layout/" + louts[i], null, null);
                 setContentView(findId);
                 i=i+1;}

              if (i==3){
                  mSensorManager.unregisterListener(this);
                  i=0;
                  checkWin(choice);
              }
          }


    }


}

这可能是this案件中的误报,也可能是我做错了什么。活动继续不停。在我调用UseSensor的主要活动中,我再次使用SensorManager。 代码片段是:

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
         if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
              // Success! There's an accelerometer sensor       
             Intent intent= new Intent(this,UseSensor.class);
             startActivity(intent);

         }

这是否与错误有关?

感谢您的时间。

---------- EDIT ------------------------

错误仍然存​​在,但它只出现在我第一次从eclipse运行应用程序。如果我停止应用程序并再次从我的手机主屏幕再次运行它没有出现。应用程序有时停止工作,但没有给我任何其他错误。我改变了我的代码,稍微添加了另一个

if(choice !=0){
          if (linear_acceleration[1]<-4.0){
             if(i<3){
                 int findId = this.getResources().getIdentifier("com.example.rockpaperaccelerate:layout/" + louts[i], null, null);
                 setContentView(findId);
                 i=i+1;}

              if (i==3){
                  mSensorManager.unregisterListener(this);

                  i=0;
                  checkWin(choice);
              }
          }
        }

我也尝试了Lukas建议here没有运气。我还实现了我的应用程序的另一个版本,只有一个活动,问题仍然存在。唯一发生的事情是,有时候应用程序停止工作而没有任何其他logCat错误

第二次实施

public class RockPaper3 extends Activity implements SensorEventListener {


    private SensorManager mSensorManager;
    private Sensor mSensor;
    public int choice=0;
    public ImageView myImage1,myImage2; 
    public TextView myTextView;
    public String louts[]={"rock","paper","scissors"};
    public int i=0;
    public float gravity[]={0,0,0}; 
    public float linear_acceleration[]={0,0,0};
    public boolean withSensor=true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


            mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
             if (mSensor != null){
                  // Success! There's an accelerometer sensor       
                setContentView(R.layout.rock_paper_accelerate_with_sensors);
                 withSensor=true;


             }
             else {

                    mSensorManager.unregisterListener(this);
                    setContentView(R.layout.activity_rock_paper3);
                    withSensor=false;
                  }      


    }

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.radio1:
                if (checked)
                    // player chose rock
                    choice=1;

                break;              
            case R.id.radio2:
                if (checked)
                    // player chose paper
                    choice=2;

                break;
            case R.id.radio3:
                if (checked)
                    // player chose scissors
                    choice=3;

                break;  
        }

        if (withSensor==false){
            checkWin(choice);
        }

    }


public void checkWin(int choice){

        Random generator = new Random();
        int machine = generator.nextInt( 3 )+1;
        setContentView(R.layout.show_results);
        myImage1 = (ImageView) findViewById(R.id.imageView1);
        myImage2 = (ImageView) findViewById(R.id.imageView2);
        myTextView = (TextView) findViewById(R.id.textView1);
        switch (machine){
        case 1: myImage1.setImageResource(R.drawable.rock);
        break;
        case 2: myImage1.setImageResource(R.drawable.paper);
        break;
        case 3: myImage1.setImageResource(R.drawable.scissors);
        break;
        }

        switch (choice){
        case 1: myImage2.setImageResource(R.drawable.rock);
        break;
        case 2: myImage2.setImageResource(R.drawable.paper);
        break;
        case 3: myImage2.setImageResource(R.drawable.scissors);
        break;
        }

        if (choice==machine){
            myTextView.setText("This is a tie!");
        }
        else if ((choice==3 && machine==2)||(choice==2 && machine==1)||(choice==1 && machine==3)){
            myTextView.setText("You Win!");
        }
        else{
            myTextView.setText("You Lose!");
        }
        Button myButton =  (Button)findViewById(R.id.button1);
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                i=0;
                Intent intent= new Intent(v.getContext(), RockPaper3.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                finish();
                startActivity(intent);


            }
        });


    }


    /*
public void checkWin(int choice){

        Random generator = new Random();
        int machine = generator.nextInt( 3 )+1;
        setContentView(R.layout.show_results);
        myWebView1 = (WebView) findViewById(R.id.webView1);
        myWebView2 = (WebView) findViewById(R.id.webView2);
        myTextView = (TextView) findViewById(R.id.textView1);
        switch (machine){
        case 1: myWebView1.loadUrl("file:///android_asset/1.jpg");
        break;
        case 2: myWebView1.loadUrl("file:///android_asset/2.jpg");
        break;
        case 3: myWebView1.loadUrl("file:///android_asset/3.jpg");
        break;
        }

        switch (choice){
        case 1: myWebView2.loadUrl("file:///android_asset/1.jpg");
        break;
        case 2: myWebView2.loadUrl("file:///android_asset/2.jpg");
        break;
        case 3: myWebView2.loadUrl("file:///android_asset/3.jpg");
        break;
        }

        if (choice==machine){
            myTextView.setText("This is a tie!");
        }
        else if ((choice==3 && machine==2)||(choice==2 && machine==1)||(choice==1 && machine==3)){
            myTextView.setText("You Win!");
        }
        else{
            myTextView.setText("You Lose!");
        }
        Button myButton =  (Button)findViewById(R.id.button1);
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                i=0;
                Intent intent= new Intent(v.getContext(), RockPaper3.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                finish();
                startActivity(intent);


            }
        });


    }*/ 

    @Override
    protected void onResume(){
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
        super.onResume();

    }

    @Override
    protected void onPause(){
        mSensorManager.unregisterListener(this);
        super.onPause(); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.rock_paper3, menu);
        return true;
    }


    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {


        final float alpha = (float) 0.8;

        gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];

        linear_acceleration[1] = event.values[1] - gravity[1];

        if(choice !=0){
          if (linear_acceleration[1]<-4.0){
             if(i<3){
                 int findId = this.getResources().getIdentifier("com.example.rockpaperv3:layout/" + louts[i], null, null);
                 setContentView(findId);
                 i=i+1;}

              if (i==3){
                          mSensorManager.unregisterListener(this);
                          i=0;
                          checkWin(choice);
                        }
                }
            }

    }
}

任何想法这个错误意味着什么以及为什么我的应用程序有时会崩溃而没有任何错误?在我看来,我的手机可能太沉重,无法使用所有加速度计注册来处理这个写得不好的应用程序。


EDIT2

注册监听器

时发生错误

EDIT3

经过大量的更改后,应用程序运行顺利,但错误始终存在。听众注册时一定是误报

1 个答案:

答案 0 :(得分:1)

将您的代码更改为:

在onResume中

你应该注册你的听众

在onPause中取消注册您的监听器

制作一个布尔isActive,

并将您的onclicklistener更改为:

Button myButton =  (Button)findViewById(R.id.button1);
    myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            i=0;
            isActive=true;
        }
    });

将onSensorChanged更改为:

@Override
public void onSensorChanged(SensorEvent event) {
    if(!isActive)return;

    final float alpha = (float) 0.8;

    gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];

    linear_acceleration[1] = event.values[1] - gravity[1];

      if (linear_acceleration[1]<-4.0){
         if(i<3){
             int findId = this.getResources().getIdentifier("com.example.rockpaperaccelerate:layout/" + louts[i], null, null);
             setContentView(findId);
             i=i+1;}

          if (i==3){
      isActive=false;
              i=0;
              checkWin(choice);
          }
      }
}

编辑,或者您按照以下代码执行此操作,但我真的不推荐您 将您的点击监听器更改为:

    myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            i=0;
     mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);

        }
    });