振动的按钮使Android应用程序强制关闭

时间:2012-08-15 20:07:30

标签: java android forceclose vibrate

我只是制作一个简单的应用程序,当单击按钮时会振动,但由于某种原因,当我点击按钮时,应用程序说它意外停止并需要强制关闭,下面是主java文件的源代码和我在清单中使用了android vibrate权限。有人可以告诉我为什么每次我点击振动按钮它会给我一个意外停止的错误吗?

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

2 个答案:

答案 0 :(得分:3)

如果您使用vibrateMe()

,则必须将vibrateMe(View v)更改为android:onClick="vibrateMe"
  

例如,如果你指定android:onClick =“sayHello”,你必须   声明上下文的公共void voidHello(View v)方法   (通常是你的活动)。

Check the developer page

答案 1 :(得分:0)

public void stopVibrating(Vibrator vibrate){

    vibrate.cancel();
}

删除它,然后检查。