首页 > 解决方案 > 开启震动、静音、正常时崩溃

问题描述

当我尝试切换手机模式时,我的应用程序崩溃了。例如,当我处于响铃模式结束时,我尝试打开静音时会崩溃,但是当我尝试切换振动时工作正常。当我尝试从振动变为响铃时,完全相同的事情正在发生。检查状态工作正常,没有崩溃。

这是 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>

标签: javaandroidandroid-studio

解决方案


推荐阅读