java - 开启震动、静音、正常时崩溃
问题描述
当我尝试切换手机模式时,我的应用程序崩溃了。例如,当我处于响铃模式结束时,我尝试打开静音时会崩溃,但是当我尝试切换振动时工作正常。当我尝试从振动变为响铃时,完全相同的事情正在发生。检查状态工作正常,没有崩溃。
这是 logcat 行:
at com.example.lab3_1.MainActivity.onSilent(MainActivity.java:56)
这涉及代码行:
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
这是主要的:
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private AudioManager myAudioManager;
NotificationManager notificationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
if(notificationManager.isNotificationPolicyAccessGranted())
{
Intent intent = new
Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
}
public void onMode(View v)
{
int mod=myAudioManager.getRingerMode();
if(mod==AudioManager.RINGER_MODE_VIBRATE){
Toast.makeText(this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
}
else if(mod==AudioManager.RINGER_MODE_NORMAL){
Toast.makeText(this,"Now in Ringing Mode",Toast.LENGTH_LONG).show();
}
else if(mod==AudioManager.RINGER_MODE_SILENT)
{
Toast.makeText(this,"Now in Silent Mode",Toast.LENGTH_LONG).show();
}
}
public void onRing(View v)
{
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(this,"Now in Ringing Mode",Toast.LENGTH_LONG).show();
}
public void onSilent(View v)
{
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(this,"Now in silent Mode",Toast.LENGTH_LONG).show();
}
public void onVibrate(View v)
{
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Toast.makeText(this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
}
}
这是xml文件:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/bt1"
android:text="CURRENT MODE CHECK"
android:onClick="onMode"/>
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/bt2"
android:text="RINGER MODE"
android:onClick="onRing"/>
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/bt3"
android:text="SILENT MODE"
android:onClick="onSilent"/>
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/bt4"
android:text="VIBRATE MODE"
android:onClick="onVibrate"/>
</LinearLayout>
解决方案
推荐阅读
- excel - 多个从属组合框Excel VBA
- javascript - 替换元素内容的函数说我的元素为空
- excel - 如何设置日值;在 Excel-VBA 中使用 Selenum-Chrome-Webdriver
- python - Attempting to nest several frames within a single frame with Tkinter. How do I accomplish this in an object oriented fashion?
- vb.net - 使用openxml 2.5的表格单元格中的对齐段落
- python - 我如何用 int 替换列表中的 None 值?
- r - 根据图案和熔体排除变量
- android - 如何分析 Android(AOSP) 构建性能?
- asp.net-core - 将所有请求从 ASP.NET Core 代理到本地节点服务器时遇到问题
- flutter - FlatButton 的 onPressed 为从 TextField 的 onChange 函数设置值的变量打印 null