2014-04-15 65 views
0

我正在嘗試製作一個應用程序,該應用程序使用加速計來檢測用戶的移動並觸發一些事件。該應用程序需要很多工作,但每次我在手機中運行應用程序時,都會收到錯誤「線程啓動」,但似乎沒有任何事情發生。SensorManager「線程啓動」錯誤

這裏是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= [email protected] 
    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); 

     } 

這是否有錯誤怎麼辦?

謝謝你的時間。

----------編輯------------------------

錯誤仍然存​​在,但它只出現我第一次從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); 
       } 
      } 
     } 

我也試了一下盧卡斯建議here沒有luck.I也實現我的應用程序的另一個版本,只有一個活動,問題依然存在。出現這種情況的唯一的事情是,有時應用程序停止工作,沒有任何其他的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

做了很大的改動應用工程進展順利,但誤差總是存在後發生。當註冊偵聽

+0

是否有意義註銷並重新註冊您的聽衆?對我來說,但我不知道你的理由,更有意義的是你不聽這些事件。你能告訴我你註冊和取消註冊傳感器的原因是什麼? – lukas

+0

實際上是的!在三次獲得if要求之後,我希望應用程序調用checkWin()並允許用戶選擇是否要繼續玩。我不認爲這是最好的實現,但如果沒有它,如果用戶移動手機,則循環再次運行。你覺得它與錯誤有關嗎?歡迎提供任何建議 –

+0

我的解決方案是否有效? – lukas

回答

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

     } 
    }); 
+0

Lukas我剛剛看到它。非常感謝您的回答。問題似乎仍然存在(不斷收到該線程啓動錯誤)。此外,現在該應用程序無法正常工作。在我做出選擇並開始移動之後,我得到一個空白屏幕。 只是爲了確保我加 \t保護無效的onResume(){ \t \t mSensorManager.registerListener(這一點,mSensor,SensorManager.SENSOR_DELAY_NORMAL); \t \t super.onResume(); \t} \t \t保護無效的onPause(){ \t \t mSensorManager.unregisterListener(本); \t \t super.onPause(); \t} 在你的編輯中,你只是建議我改變onClickListener? –

+0

@MikeVol不要擔心錯誤。如果你的問題仍然存在,我今天晚上看看......? – lukas

+0

這是非常神祕的,因爲儘管錯誤的東西似乎工作 –