Android: Vibrator doesn't work. Foce Close emulator

雨燕双飞 提交于 2020-01-25 06:19:50

问题


hey Guys, I'm still an Android & Java noob, but everything I've seen is telling me this should totally work, but it doesn't! not in the emulator, not on the phone.. I'm trying to use the vibrator using vibrate(500); ..I get an " application stopped unexpectedly " error

what am I missing?

code below:

package com.phys;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class phys extends Activity {

 Vibrator vibr;
 Button but;
 TextView txt;
 int counter = 0;

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

        vibr = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
        but = (Button)findViewById(R.id.Button01);
        txt = (TextView)findViewById(R.id.txt);
        but.setOnClickListener(clk);
    }

    OnClickListener clk = new OnClickListener(){

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   txt.setText(Integer.toString(counter));
   //do something else
   vibr.vibrate(500);
   counter++;
  }

    };
}

回答1:


Use adb logcat, DDMS, or the DDMS perspective in Eclipse to look at LogCat and examine the stack trace associated with your " application stopped unexpectedly " error. That will give you more information about where you are going wrong.

I suspect the problem is that you are missing the VIBRATE permission. If so, add this as a child of your <manifest> element in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.VIBRATE"/>


来源:https://stackoverflow.com/questions/3604574/android-vibrator-doesnt-work-foce-close-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!