2012-08-15 5 views
1

버튼을 클릭했을 때 진동하는 간단한 앱을 만들고 있습니다 만, 버튼을 클릭 할 때 앱이 예기치 않게 멈추었 고 강제 종료 할 필요가 있다고 말하는 이유가 아래에 있습니다 소스 코드를 주 Java 파일에 저장하고 내 매니페스트에서 안드로이드 진동 사용 권한을 사용했습니다. 왜 누군가가 진동 버튼을 클릭 할 때마다 갑자기 멈춘다는 오류가 나옵니까?진동 버튼으로 안드로이드 앱을 강제 종료합니다

package com.test; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Vibrator; 
import android.view.View; 
import android.widget.EditText; 

public class Main extends Activity { 

public final static String EXTRA_MESSAGE = "com.test.MESSAGE"; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

/* Called when the user clicks the button */ 
public void sendMessage(View view) { 
    // do something in response to button 
    Intent intent = new Intent(this, DisplayMessageActivity.class); 
    EditText editText = (EditText) findViewById(R.id.edit_message); 
    String message = editText.getText().toString(); 
    intent.putExtra(EXTRA_MESSAGE, message); 
    startActivity(intent); 
} 

public void vibrateMe() { 


    Vibrator vibrate = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); 

    vibrate.vibrate(500); 


} 

public void stopVibrating(Vibrator vibrate) { 

     vibrate.cancel(); 
    } 
} 
+0

스택 트레이스를 추가, 당신은 아마 진동 권한을 추가하지 않았다. – Jug6ernaut

+0

진동 허가를 사용했지만 여전히 동일하게 유지됩니다 –

+0

진동기가 맞습니까? logcat을 확인하면 대개 어떤 코드 행이 문제인지를 알려줍니다. 진동에 대한 참조를 잃을 수도 있습니다. 어디에서 vibrateMe()와 stopVibrating이 호출됩니까? –

답변

3

당신은 당신이 예를 들어 android:onClick="vibrateMe"

를 사용하는 경우가 안드로이드 지정하면, vibrateMe(View v)vibrateMe()을 변경해야합니다 : 온 클릭이 = "sayHello가가"당신이 의 공공 무효의 sayHello (보기 V) 메소드를 선언해야 귀하의 컨텍스트 (일반적으로 귀하의 활동).

Check the developer page

0

공공 무효 stopVibrating (진동 진동) {

vibrate.cancel(); 
} 

이를 제거하고 확인합니다.

관련 문제