gpt4 book ai didi

java - context=this 让我的应用程序停止工作

转载 作者:行者123 更新时间:2023-11-29 03:39:10 25 4
gpt4 key购买 nike

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)

最佳答案

正如您的 logcat 结果所说:

java.lang.SecurityException: Requires VIBRATE permission 12-20

意味着您需要在 AndroidManifest.xml 中添加 VIBRATE 权限:

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

关于java - context=this 让我的应用程序停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13958166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com