英文:
Attempt to invoke virtual method 'java.lang.Object android.content.Context
问题
guys我创建了这个新类,但是当我在MainActivity中调用这个vibrate函数时,出现了错误
public class vibrate extends Activity {
public void vibrate() {
try {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// 震动500毫秒
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
// 在API 26中已弃用
v.vibrate(200);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我调用这个类的方式
vibrate v = new vibrate();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
v.vibrate();
}
这是错误信息
W/System.err: java.lang.NullPointerException: 尝试调用空对象引用上的 'java.lang.Object android.content.Context.getSystemService(java.lang.String)'
英文:
guys i make this new class but when i call this function vibrate in Main Activity i have error
public class vibrate extends Activity{
public void vibrate() {
try {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
//deprecated in API 26
v.vibrate(200);
}
} catch (Exception e) {
e.printStackTrace();
}
}
and this how i called this class
vibrate v = new vibrate() ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
v.vibrate();
}
this is the error
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
答案1
得分: 1
Class vibrate
是 Activity 的子类,当你像这样创建一个实例时,你正在创建一个 activity。但这不是正确的创建 activity 的方式。
vibrate v = new vibrate()
你必须在 AndroidManifest.xml
文件中声明你的 activity,并通过使用 intent 来调用该 activity。
Intent intent = new Intent(this, vibrate.class);
startActivity(intent);
但是要使用 Vibrator,你只需要这样做。
public class VibrateActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vibrate();
}
private void vibrate() {
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null) {
// 震动 500 毫秒
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
// 在 API 26 中已弃用
vibrator.vibrate(200);
}
}
}
}
不要忘记在 AndroidManifest.xml
文件中包含权限:
<uses-permission android:name="android.permission.VIBRATE"/>
更新:
在不在 activity 中执行的情况下使用单独的类。
public class Vibrate extends Activity {
public void vibrate(Context context) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null) {
// 震动 500 毫秒
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
// 在 API 26 中已弃用
vibrator.vibrate(200);
}
}
}
}
然后在你的 activity 中像这样使用它。
Vibrate vibrate = new Vibrate();
vibrate.vibrate(this);
英文:
Class vibrate
is a subclass of Activity, when you create an instance like this, you are creating an activity. But it's not a right way to create an activity.
vibrate v = new vibrate()
You have to declare your activity inside AndroidManifest.xml
file, and invoke the activity by using intent.
Intent intent = new Intent(this, vibrate.class);
startActivity(intent);
But to use the Vibrator, you just need to do like this.
public class VibrateActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vibrate();
}
private void vibrate() {
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null) {
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
//deprecated in API 26
v.vibrate(200);
}
}
}
Don't forget to include permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.VIBRATE"/>
UPDATE:
Use separate class without doing in the activity.
public class Vibrate extends Activity {
public void vibrate(Context context) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null) {
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
//deprecated in API 26
v.vibrate(200);
}
}
}
And use it like this in your activity.
Vibrate vibrate = new Vibrate();
vibrate.vibrate(this)
答案2
得分: 0
在onCreate()中初始化vibrate v = new vibrate();并调用v.vibrate();不需要将vibrate作为一个Activity。
英文:
Initialize vibrate v = new vibrate(); in onCreate()
and call v.vibrate();
No need to make vibrate as an Activity.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论