我正在关注本书的一些教程,我的代码看起来很好,因为我没有任何错误。但是当我尝试运行我的应用程序时,我遇到了这些错误:
02-04 15:22:29.379: E/Trace(10992): error opening trace file: No such file or directory (2)
02-04 15:22:29.379: D/ActivityThread(10992): setTargetHeapUtilization:0.25
02-04 15:22:29.389: D/ActivityThread(10992): setTargetHeapIdealFree:8388608
02-04 15:22:29.389: D/ActivityThread(10992): setTargetHeapConcurrentStart:2097152
02-04 15:22:29.429: W/dalvikvm(10992): VFY: unable to resolve virtual method 15273: Lcom/example/silentmodetoggle/MainActivity;.getDrawable (I)Landroid/graphics/drawable/Drawable;
02-04 15:22:29.429: W/dalvikvm(10992): VFY: unable to resolve virtual method 15273: Lcom/example/silentmodetoggle/MainActivity;.getDrawable (I)Landroid/graphics/drawable/Drawable;
02-04 15:22:29.479: W/dalvikvm(10992): threadid=1: thread exiting with uncaught exception (group=0x41540438)
02-04 15:22:29.479: E/AndroidRuntime(10992): FATAL EXCEPTION: main
02-04 15:22:29.479: E/AndroidRuntime(10992): java.lang.NoSuchMethodError: com.example.silentmodetoggle.MainActivity.getDrawable
02-04 15:22:29.479: E/AndroidRuntime(10992): at com.example.silentmodetoggle.MainActivity.toggleUi(MainActivity.java:75)
02-04 15:22:29.479: E/AndroidRuntime(10992): at com.example.silentmodetoggle.MainActivity.onResume(MainActivity.java:88)
02-04 15:22:29.479: E/AndroidRuntime(10992): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1187)
02-04 15:22:29.479: E/AndroidRuntime(10992): at android.app.Activity.performResume(Activity.java:5314)
02-04 15:22:29.479: E/AndroidRuntime(10992): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2578)
02-04 15:22:29.479: E/AndroidRuntime(10992): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2616)
02-04 15:22:29.479: E/AndroidRuntime(10992): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2102)
02-04 15:22:29.479: E/AndroidRuntime(10992): at android.app.ActivityThread.access$600(ActivityThread.java:137)
02-04 15:22:29.479: E/AndroidRuntime(10992): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
02-04 15:22:29.479: E/AndroidRuntime(10992): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 15:22:29.479: E/AndroidRuntime(10992): at android.os.Looper.loop(Looper.java:137)
02-04 15:22:29.479: E/AndroidRuntime(10992): at android.app.ActivityThread.main(ActivityThread.java:4863)
02-04 15:22:29.479: E/AndroidRuntime(10992): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 15:22:29.479: E/AndroidRuntime(10992): at java.lang.reflect.Method.invoke(Method.java:511)
02-04 15:22:29.479: E/AndroidRuntime(10992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-04 15:22:29.479: E/AndroidRuntime(10992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-04 15:22:29.479: E/AndroidRuntime(10992): at dalvik.system.NativeStart.main(Native Method)
我不知道哪个部分出了问题。
这是我的代码:
package com.example.silentmodetoggle;
import android.app.Activity;
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 Activity {
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
checkIfPhoneIsSilent();
setButtonClickListener();
}
private void setButtonClickListener() {
Button toggleButton = (Button)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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();
}
});
}
/*
* 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 = getDrawable(R.drawable.phone_silent);
} else {
newPhoneImage = getDrawable(R.drawable.phone_on);
}
imageView.setImageDrawable(newPhoneImage);
}
@Override
protected void onResume() {
super.onResume();
checkIfPhoneIsSilent();
toggleUi();
}
}
我在API 23上运行,我正在使用Eclipse。你能帮我确定一下我的错误是什么吗?我是Android的新手。
答案 0 :(得分:2)
这是您的代码崩溃的地方: java.lang.NoSuchMethodError:com.example.silentmodetoggle.MainActivity.getDrawable
您应该在资源对象上调用getDrawable: getResources()。getDrawable(
答案 1 :(得分:2)
getDrawable(resid)
,而选择getDrawable(resid, theme);
试试这个,
private Drawable getDrawableResource(int resID) {
return ContextCompat.getDrawable(this, resID);
}
然后在你的代码中,
if(mPhoneIsSilent) {
newPhoneImage = getDrawableResource(R.drawable.phone_silent);
} else {
newPhoneImage = getDrawableResource(R.drawable.phone_on);
}
ContextCompat.getDrawable(this, resID)
相当于调用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(resID, context.getTheme());
} else {
return resources.getDrawable(resID);
}
答案 2 :(得分:1)
仅编辑此方法: -
private void toggleUi() {
ImageView imageView = (ImageView)findViewById(R.id.imageView);
if(mPhoneIsSilent) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.img1));
} else {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.img2));
}
}