1

context=this使我的应用程序停止工作。怎么了?

package com.robot.soccer.control;
import com.robot.soccer.control.R;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Vibrator;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;


public class Main extends Activity implements OnClickListener, AccelerometerListener {

TextView ipText;
 TextView portText;
 TextView status;
 TextView xStatus;
 TextView yStatus;
 TextView zStatus;
 TextView accText;

 ImageButton goBut;
 ImageButton kickBut;
 public float xVal;
 public float yVal;

private static Context CONTEXT;
    @Override
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        accText=(TextView)this.findViewById(R.id.accm);
        xStatus=(TextView)this.findViewById(R.id.x);
        yStatus=(TextView)this.findViewById(R.id.y);
        zStatus=(TextView)this.findViewById(R.id.z);
        status=(TextView)this.findViewById(R.id.status);
        portText = (TextView)this.findViewById(R.id.portText);
        ipText=(TextView)this.findViewById(R.id.ipText);
        goBut=(ImageButton)this.findViewById(R.id.goBut);
        kickBut=(ImageButton)this.findViewById(R.id.kickBut);
        goBut.setOnClickListener(this);
        kickBut.setOnClickListener(this);
        accText.setText("AccelerometerStatus ");
        //CONTEXT=this;

    }

这是我的日志:

12-20 01:22:50.271: E/AndroidRuntime(281): FATAL EXCEPTION: main
12-20 01:22:50.271: E/AndroidRuntime(281): java.lang.SecurityException: Requires VIBRATE permission
12-20 01:22:50.271: E/AndroidRuntime(281):  at android.os.Parcel.readException(Parcel.java:1247)
12-20 01:22:50.271: E/AndroidRuntime(281):  at android.os.Parcel.readException(Parcel.java:1235)
12-20 01:22:50.271: E/AndroidRuntime(281):  at android.os.IVibratorService$Stub$Proxy.vibrate(IVibratorService.java:106)
12-20 01:22:50.271: E/AndroidRuntime(281):  at android.os.Vibrator.vibrate(Vibrator.java:44)
12-20 01:22:50.271: E/AndroidRuntime(281):  at com.robot.soccer.control.Main.vibrate(Main.java:253)
12-20 01:22:50.271: E/AndroidRuntime(281):  at com.robot.soccer.control.Main.OnAccelerationChanged(Main.java:239)
12-20 01:22:50.271: E/AndroidRuntime(281):  at com.robot.soccer.control.AccelerometerManager$1.onSensorChanged(AccelerometerManager.java:135)
12-20 01:22:50.271: E/AndroidRuntime(281):  at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:449)
12-20 01:22:50.271: E/AndroidRuntime(281):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 01:22:50.271: E/AndroidRuntime(281):  at android.os.Looper.loop(Looper.java:123)
12-20 01:22:50.271: E/AndroidRuntime(281):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-20 01:22:50.271: E/AndroidRuntime(281):  at java.lang.reflect.Method.invokeNative(Native Method)
12-20 01:22:50.271: E/AndroidRuntime(281):  at java.lang.reflect.Method.invoke(Method.java:521)
12-20 01:22:50.271: E/AndroidRuntime(281):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-20 01:22:50.271: E/AndroidRuntime(281):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-20 01:22:50.271: E/AndroidRuntime(281):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2
 java.lang.SecurityException: Requires VIBRATE permission

将 VIBRATE 权限添加到您的清单。

http://developer.android.com/reference/android/Manifest.permission.html#VIBRATE

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

于 2012-12-19T18:28:15.087 回答
2

正如您的 logcat 结果所说:

java.lang.SecurityException:需要振动权限 12-20

意味着您需要将 VIBRATE 权限添加AndroidManifest.xml为:

<uses-permission android:name="android.permission.VIBRATE" />
于 2012-12-19T18:28:54.987 回答