我制作了一个简单的Android应用程序,用户可以通过单击按钮在静音模式和静音模式之前的状态之间切换,但在模拟器或Android设备上运行应用程序时消息&#34 ;应用程序意外停止"正在显示。 我的代码出了什么问题。
P.S。:我复制了Android for Dummies的代码,稍作修改,比如更改照片的名称。
这是我的代码: package com.example.silentmodetoggle;
import android.widget.Button;
import android.media.AudioManager;
import android.view.*;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageView;
import android.graphics.drawable.Drawable;
public class MainActivity extends Activity{
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setButtonClickListener();
checkIfPhoneIsSilent();
mAudioManager=(AudioManager)getSystemService(AUDIO_SERVICE);
}
private void setButtonClickListener(){
Button toggleButton=(Button)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPhoneIsSilent){
//change back to normal mode
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mPhoneIsSilent=false;
}
else
{
//change to silent mode
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mPhoneIsSilent=true;
}
//now toggle the UI again
toggleUi();
}
});
}
private void checkIfPhoneIsSilent(){
int ringerMode=mAudioManager.getRingerMode();
if(ringerMode==AudioManager.RINGER_MODE_SILENT){
mPhoneIsSilent=true;}
else
mPhoneIsSilent=false;
}
// Toggles the UI images from silent to normal and vice versa.
private void toggleUi()
{
ImageView imageView=(ImageView)findViewById(R.id.phone_icon);
Drawable newPhoneImage;
if(mPhoneIsSilent){
newPhoneImage=getResources().getDrawable(R.drawable.silent);
}
else{
newPhoneImage=getResources().getDrawable(R.drawable.ringer);
}
imageView.setImageDrawable(newPhoneImage);
}
@Override
protected void onResume() {
super.onResume();
checkIfPhoneIsSilent();
toggleUi();
}
}
这是AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/phone_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:contentDescription="@+id/desc"
android:src="@drawable/ringer" />
<Button
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/phone_icon"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Toggle_Silent_Mode" />
</RelativeLayout>
以下内容显示在LogCat中:
12-29 21:21:31.475: E/AndroidRuntime(821): FATAL EXCEPTION: main
12-29 21:21:31.475: E/AndroidRuntime(821): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.silentmodetoggle/com.example.silentmodetoggle.MainActivity}: java.lang.NullPointerException
12-29 21:21:31.475: E/AndroidRuntime(821): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-29 21:21:31.475: E/AndroidRuntime(821): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-29 21:21:31.475: E/AndroidRuntime(821): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-29 21:21:31.475: E/AndroidRuntime(821): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-29 21:21:31.475: E/AndroidRuntime(821): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 21:21:31.475: E/AndroidRuntime(821): at android.os.Looper.loop(Looper.java:123)
12-29 21:21:31.475: E/AndroidRuntime(821): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-29 21:21:31.475: E/AndroidRuntime(821): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 21:21:31.475: E/AndroidRuntime(821): at java.lang.reflect.Method.invoke(Method.java:521)
12-29 21:21:31.475: E/AndroidRuntime(821): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-29 21:21:31.475: E/AndroidRuntime(821): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-29 21:21:31.475: E/AndroidRuntime(821): at dalvik.system.NativeStart.main(Native Method)
12-29 21:21:31.475: E/AndroidRuntime(821): Caused by: java.lang.NullPointerException
12-29 21:21:31.475: E/AndroidRuntime(821): at com.example.silentmodetoggle.MainActivity.checkIfPhoneIsSilent(MainActivity.java:58)
12-29 21:21:31.475: E/AndroidRuntime(821): at com.example.silentmodetoggle.MainActivity.onCreate(MainActivity.java:23)
12-29 21:21:31.475: E/AndroidRuntime(821): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-29 21:21:31.475: E/AndroidRuntime(821): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
答案 0 :(得分:1)
你正在调用checkIfPhoneIsSilent(),它在你设置mAudioManager之前使用了mAudioManager,所以它是null,你得到一个NullPointerException。
答案 1 :(得分:1)
在实例化AudioManager之前调用checkIfPhoneIsSilent。这会导致NullPointerException。更改onCreate方法以解决此问题:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAudioManager=(AudioManager)getSystemService(AUDIO_SERVICE);
setButtonClickListener();
checkIfPhoneIsSilent();
}
答案 2 :(得分:0)
更改此代码:
mAudioManager=(AudioManager)getSystemService(AUDIO_SERVICE);
setButtonClickListener();
checkIfPhoneIsSilent();