我是Java開發者中的一名成員。我正在閱讀一本名爲「傻瓜式Android應用程序開發」的書。在本書的第5章中,給出了下面的代碼片斷,它不起作用。錯誤:(90,58)錯誤:無法找到可繪製的符號變量
有人可以幫助我知道我在代碼中做錯了什麼嗎? 所有幫助表示讚賞。
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
null。
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) 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) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 在dalvik.system .NativeStart.main(原生實現方法具 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.re s.Resources $ NotFoundException:文件RES /繪製
/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) 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。 (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
The above is the Exception Logcat which I found very difficult to paste However, somehow I managed. Your updated and edited answer is giving an error at the "else" block.
您好RISHABH,感謝您的快速回復。我如何在代碼中附加錯誤的屏幕截圖。屏幕找不到phone_silent.png和phone_on.png。 – maneeshmassey
現在應該做什麼以進一步進行?謝謝。 – maneeshmassey
'phone_silent.png'和'phone_on.png'應放置在'app> src> main> res> drawable'文件夾中。如果你有更多的切口,然後根據它們的分辨率/密度將它們放在'drawable-hdpi,mdpi,xhdpi,xxhdpi'中。我看不到你的截圖。 –