1

以下代码在 ICS 上正确运行,但是当我为 Gingerbread 尝试它时,它产生了一个错误

        AlertDialog.Builder alert3 = new AlertDialog.Builder(ReadingClass.this);
        alert3.setIcon(R.drawable.pwi);
        alert3.setTitle("Primewater");
        alert3.setMessage("Meter Reading");

        final Context context3 = alert3.getContext();

        // Set an EditText view to get user input 

        final LinearLayout layout3 = new LinearLayout(context3);

logcat 堆栈跟踪如下

06-10 11:39:19.459: E/AndroidRuntime(7922): FATAL EXCEPTION: main
06-10 11:39:19.459: E/AndroidRuntime(7922): java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.getContext
06-10 11:39:19.459: E/AndroidRuntime(7922):     at com.primereader.ReadingClass.onClick(ReadingClass.java:173)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at android.view.View.performClick(View.java:2485)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at android.view.View$PerformClick.run(View.java:9080)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at android.os.Handler.handleCallback(Handler.java:587)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at android.os.Looper.loop(Looper.java:130)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at java.lang.reflect.Method.invokeNative(Native Method)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at java.lang.reflect.Method.invoke(Method.java:507)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-10 11:39:19.459: E/AndroidRuntime(7922):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

AlertDialog.Builder.getContext()是在 API 级别 11 (Honeycomb) 中引入的,因此此方法在 Gingerbread 上不可用。

于 2013-06-10T04:09:30.787 回答