每当我这样做时,无论我从哪里调用它,应用程序都会崩溃 IllegalStateException:无法执行活动的方法。我目前只有几个类,它们都在同一个包中,包括带有导致崩溃的静态方法的类。
在Android应用中调用静态方法时是否应该考虑一些问题或特殊注意事项?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ImageManager.initImageManager(this); // crash
}
修改
道歉,错误实际上是 VerifyError 。似乎与Dropbox API存在一些冲突,我正在其中一个静态方法中使用它,但在应用程序崩溃之前不会调用它。在我将ImageManager更改为完全静态的类之后,以下是错误输出,我认为它应该是。 ImageManager代码如下:
01-09 19:23:51.356: W/dalvikvm(32032): VFY: unable to find class referenced in signature (Lcom/dropbox/client2/DropboxAPI;)
01-09 19:23:51.357: I/dalvikvm(32032): Could not find method com.dropbox.client2.DropboxAPI.getFile, referenced from method rfynes.moviebrowser.ImageManager.downloadImage
01-09 19:23:51.357: W/dalvikvm(32032): VFY: unable to resolve virtual method 5140: Lcom/dropbox/client2/DropboxAPI;.getFile (Ljava/lang/String;Ljava/lang/String;Ljava/io/OutputStream;Lcom/dropbox/client2/ProgressListener;)Lcom/dropbox/client2/DropboxAPI$DropboxFileInfo;
01-09 19:23:51.357: D/dalvikvm(32032): VFY: replacing opcode 0x6e at 0x0063
01-09 19:23:51.358: W/dalvikvm(32032): VFY: unable to resolve exception class 735 (Lcom/dropbox/client2/exception/DropboxServerException;)
01-09 19:23:51.358: W/dalvikvm(32032): VFY: unable to find exception handler at addr 0x8b
01-09 19:23:51.358: W/dalvikvm(32032): VFY: rejected Lrfynes/moviebrowser/ImageManager;.downloadImage (Ljava/lang/String;Ljava/io/File;Ljava/lang/String;Lcom/dropbox/client2/DropboxAPI;)V
01-09 19:23:51.358: W/dalvikvm(32032): VFY: rejecting opcode 0x0d at 0x008b
01-09 19:23:51.358: W/dalvikvm(32032): VFY: rejected Lrfynes/moviebrowser/ImageManager;.downloadImage (Ljava/lang/String;Ljava/io/File;Ljava/lang/String;Lcom/dropbox/client2/DropboxAPI;)V
01-09 19:23:51.358: W/dalvikvm(32032): Verifier rejected class Lrfynes/moviebrowser/ImageManager;
01-09 19:23:51.358: D/AndroidRuntime(32032): Shutting down VM
01-09 19:23:51.358: W/dalvikvm(32032): threadid=1: thread exiting with uncaught exception (group=0x41672898)
01-09 19:23:51.361: E/AndroidRuntime(32032): FATAL EXCEPTION: main
01-09 19:23:51.361: E/AndroidRuntime(32032): java.lang.VerifyError: rfynes/moviebrowser/ImageManager
01-09 19:23:51.361: E/AndroidRuntime(32032): at rfynes.moviebrowser.MainActivity.onCreate(MainActivity.java:16)
01-09 19:23:51.361: E/AndroidRuntime(32032): at android.app.Activity.performCreate(Activity.java:5150)
01-09 19:23:51.361: E/AndroidRuntime(32032): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-09 19:23:51.361: E/AndroidRuntime(32032): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2192)
01-09 19:23:51.361: E/AndroidRuntime(32032): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2288)
01-09 19:23:51.361: E/AndroidRuntime(32032): at android.app.ActivityThread.access$600(ActivityThread.java:148)
01-09 19:23:51.361: E/AndroidRuntime(32032): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1273)
01-09 19:23:51.361: E/AndroidRuntime(32032): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 19:23:51.361: E/AndroidRuntime(32032): at android.os.Looper.loop(Looper.java:137)
01-09 19:23:51.361: E/AndroidRuntime(32032): at android.app.ActivityThread.main(ActivityThread.java:5222)
01-09 19:23:51.361: E/AndroidRuntime(32032): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 19:23:51.361: E/AndroidRuntime(32032): at java.lang.reflect.Method.invoke(Method.java:525)
01-09 19:23:51.361: E/AndroidRuntime(32032): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-09 19:23:51.361: E/AndroidRuntime(32032): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-09 19:23:51.361: E/AndroidRuntime(32032): at dalvik.system.NativeStart.main(Native Method)
的ImageManager
public class ImageManager {
private File posterDir, backdropDir;
public static void initImageManager(Activity activity) {
posterDir = activity.getDir("poster", Context.MODE_PRIVATE);
backdropDir = activity.getDir("backdrop", Context.MODE_PRIVATE);
}
...
...
public static void downloadImage(String movieId, File imageDir, String imageType, DropboxAPI<AndroidAuthSession> dropbox) {
...
...
答案 0 :(得分:1)
为了解决这个问题,我实际上最终将ImageManager类中的 downloadImage 方法(以及其他一些方法)分解为它们自己独立的非静态类。 ImageManager现在仍然是一个静态类,但它不会导致任何问题,并且应用程序不会崩溃。所以,问题似乎是我从静态上下文中创建的Dropbox API调用,但我不知道为什么。不过,我对现在的情况感到很满意。