2012-12-19 122 views
1

context=this使我的應用程序停止工作。怎麼了?context =這使我的應用程序停止工作

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; 

    } 

這裏是我的logcat:

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) 
+0

分配變量本身可能什麼也不做。你在用'CONTEXT'做什麼? – dmon

+5

首先從Context變量中移除靜態爲'private Context CONTEXT;'並在應用程序崩潰時也發佈日誌 –

+2

沒有什麼能夠阻止他使用靜態修改器,除非它可能是糟糕的設計選擇。 :P –

回答

2

當你的logcat的結果說:

java.lang.SecurityException異常:要求VIBRATE許可12-20

意味着你需要添加VIBRATE允許在AndroidManifest.xml作爲:

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

工作..謝謝:d –

+0

@YoGhiShicibukai:最歡迎的朋友! :)和一個建議儘量避免使用靜態字段或行爲在Android應用程序嘗試使用內置的API –