我是Java开发中的初学者。我正在读一本名为“Android应用开发傻瓜”的书。在本书的第5章中,给出了以下不起作用的代码片段。
有人可以帮我知道我在代码中做错了什么吗? 感谢所有帮助。
package helloandroid.android.dummies.com.silentmodetoggle;
import android.support.v7.app.AppCompatActivity;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setButtonClickListener();
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
checkIfPhoneIsSilent();
setButtonClickListener();
}
private void setButtonClickListener() {
Button toggleButton = (Button) findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mPhoneIsSilent) {
//Change back to silent 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();
}
});
}
/**
* Checks to see if the phone is currently in silent mode
*/
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(drawable.phone_silent
}
else {
newPhoneImage =
getResources(drawable.phone_on);
}
imageView.setImageDrawable(newPhoneImage);
}
@Override
protected void onResume() {
super.onResume();
checkIfPhoneIsSilent();
toggleUI();
}
}
有些人愿意提供帮助。 谢谢。
UPDATE
10-12 13:53:28.672 2965-2965/? D/dalvikvm: Not late-enabling CheckJNI (already on)
10-12 13:53:28.692
2965-2965/helloandroid.android.dummies.com.silentmodetoggle E/Trace: error
opening trace file: No such file or directory (2)
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle I/dalvikvm:
Could not
find method android.app.Application.registerOnProvideAssistDataListener,
referenced from method
com.android.tools.fd.runtime.BootstrapApplication.registerOnProvideAssistDataListener
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm: VFY:
unable to resolve virtual method 231: Landroid/app
/Application;.registerOnProvideAssistDataListener (Landroid/app
/Application$OnProvideAssistDataListener;)
10-12 13:53:28.703 2965-2965/helloandroid.android.dummies.com.silentmodetoggle D/dalvikvm: VFY:
replacing opcode 0x6e at 0x0002
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle I/dalvikvm: Could
not find method
android.app.Application.unregisterOnProvideAssistDataListener, referenced
from method
com.android.tools.fd.runtime.BootstrapApplication.unregisterOnProvideAssistDataLis
tener
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm: VFY:
unable to resolve virtual method 234: Landroid/app
/Application;.unregisterOnProvideAssistDataListener (Landroid/app
/Application$OnProvideAssistDataListener;)V
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle D/dalvikvm: VFY:
replacing opcode 0x6e at 0x0002
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle I/InstantRun:
Instant Run Runtime started. Android package is
helloandroid.android.dummies.com.silentmodetoggle, real application class is
空。
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/InstantRun: No
instant run dex files added to classpath
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle E/dalvikvm: Could
not find class 'android.util.ArrayMap', referenced from method
com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm: VFY:
unable to resolve check-cast 1884 (Landroid/util/ArrayMap;) in Lcom/android
/tools/fd/runtime/MonkeyPatcher;
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle D/dalvikvm: VFY:
replacing opcode 0x1f at 0x025e
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle E/dalvikvm: Could
not find class 'android.util.ArrayMap', referenced from method
com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm: VFY:
unable to resolve const-class 1884 (Landroid/util/ArrayMap;) in Lcom/android
/tools/fd/runtime/MonkeyPatcher;
10-12 13:53:28.703
2965-2965/helloandroid.android.dummies.com.silentmodetoggle D/dalvikvm: VFY:
replacing opcode 0x1c at 0x0060
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle I/dalvikvm: Could
not find method android.view.Window$Callback.onProvideKeyboardShortcuts,
referenced from method
android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm: VFY:
unable to resolve interface method 15983: Landroid/view
/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid
/view/Menu;I)V
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle D/dalvikvm: VFY:
replacing opcode 0x72 at 0x0002
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm: VFY:
unable to find class referenced in signature (Landroid/view/SearchEvent;)
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle I/dalvikvm: Could
not find method android.view.Window$Callback.onSearchRequested, referenced
from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm: VFY:
unable to resolve interface method 15985: Landroid/view
/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle D/dalvikvm: VFY:
replacing opcode 0x72 at 0x0002
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle I/dalvikvm: Could
not find method android.view.Window$Callback.onWindowStartingActionMode,
referenced from method
android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm: VFY:
unable to resolve interface method 15989: Landroid/view
/Window$Callback;.onWindowStartingActionMode (Landroid/view
/ActionMode$Callback;I)Landroid/view/ActionMode;
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle D/dalvikvm: VFY:
replacing opcode 0x72 at 0x0002
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle I/dalvikvm: Could
not find method android.content.res.TypedArray.getChangingConfigurations,
referenced from method
android.support.v7.widget.TintTypedArray.getChangingConfigurations
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm: VFY:
unable to resolve virtual method 669: Landroid/content
/res/TypedArray;.getChangingConfigurations ()I
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle D/dalvikvm: VFY:
replacing opcode 0x6e at 0x0002
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle I/dalvikvm: Could
not find method android.content.res.TypedArray.getType, referenced from
method android.support.v7.widget.TintTypedArray.getType
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm: VFY:
unable to resolve virtual method 691: Landroid/content
/res/TypedArray;.getType (I)I
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle D/dalvikvm: VFY:
replacing opcode 0x6e at 0x0008
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle
E/VdcInflateDelegate: Exception while inflating <vector
org.xmlpull.v1.XmlPullParserException: Binary XML file line #17<vector>
tag requires viewportHeight > 0
at
android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(
VectorDrawableCompat.java:544)
at
android.support.graphics.drawable.VectorDrawableCompat.inflate
(VectorDrawableCompat.java:478)
at
android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner
(VectorDrawableCompat.java:441)
at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:742)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:362)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:185)
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:720)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
at
android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:127)
at android.support.v7.app.AppCompatDelegateImplV9.<init
(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.<init>
(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.<init>
(AppCompatDelegateImplV14.java:50)
at
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
at
android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521) 在
android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at
helloandroid.android.dummies.com.silentmodetoggle.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at
android.app.ActivityThread.access$600(ActivityThread.java:130)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 在dalvik.system.NativeStart.main(Native Metho 10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle
D/AndroidRuntime: Shutting down VM
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle W/dalvikvm:
threadid=1: thread exiting with uncaught exception (group=0xb2e51288)
10-12 13:53:28.732
2965-2965/helloandroid.android.dummies.com.silentmodetoggle
E/AndroidRuntime:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
ComponentInfo{helloandroid.android.dummies.com.silentmodetoggle/helloandroid.andro
id.dummies.com.silentmodetoggle.MainActivity}:
android.content.res.Resources$NotFoundException: File res/drawable
/abc_vector_test.xml from drawable resource ID #0x7f02005
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at
android.app.ActivityThread.access$600(ActivityThread.java:13
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) 引起:android.content.res.Resources $ NotFoundException:文件res / drawable
/abc_vector_test.xml from drawable resource ID #0x7f020052
at android.content.res.Resources.loadDrawable(Resources.java:1918)
at android.content.res.Resources.getDrawable(Resources.java:659)
at
android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:346)
at
android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197) 在android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:185) 在android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:720) 在android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190) 在android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77) 在android.support.v7.app.AppCompatDelegateImplBase。 (AppCompatDelegateImplBase.java:127) 在android.support.v7.app.AppCompatDelegateImplV9。(AppCompatDelegateImplV9.java:147) 在android.support.v7.app.AppCompatDelegateImplV11。(AppCompatDelegateImplV11.java:27) 在android.support.v7.app.AppCompatDelegateImplV14。(AppCompatDelegateImplV14.java:50) 在
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at helloandroid.android.dummies.com.silentmodetoggle.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:877)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
at android.content.res.Resources.loadDrawable(Resources.java:1915)
at android.content.res.Resources.getDrawable(Resources.java:659)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:346)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:185)
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:720)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:127)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at helloandroid.android.dummies.com.silentmodetoggle.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
10-12 13:53:28.752
2965-2969/helloandroid.android.dummies.com.silentmodetoggle D/dalvikvm:
GC_CONCURRENT freed 305K, 7% free 6071K/6471K, paused 16ms+1ms, total 18ms
10-12 13:53:31.472 2965-2965/? I/Process: Sending signal. PID: 2965 SIG: 9
以上是Exception Logcat,我发现很难粘贴 但是,不知怎的,我管理了。 您更新和编辑的答案在“else”块中发出错误。
答案 0 :(得分:0)
做类似的事
private void toggleUI() {
ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
if (imageView != null) {
int imageResId = mPhoneIsSilent ? R.drawable.phone_silent : R.drawable.phone_on;
imageView.setImageResource(imageResId);
} else {
Log.e("ERR", "ImageView is null");
}
}