gpt4 book ai didi

Android minSdkVersion 和使用更高级别 SDK 中引入的类

转载 作者:行者123 更新时间:2023-11-30 04:51:12 25 4
gpt4 key购买 nike

我正在测试 android minSDKVersion 属性并发现一个奇怪的行为-

我将 minSDKVersion=3 (1.5) 和 targetSDKVersion=4 (1.6) 放在 androidManifest.xml 文件中。

为了测试,我在启动 Activity 的 onCreate 方法中添加了以下几行 -

 android.telephony.SmsManager sm = android.telephony.SmsManager.getDefault();
ArrayList<String> stringArray = sm.divideMessage("this is message");
Toast.makeText(this, stringArray.get(0), Toast.LENGTH_LONG).show();

android.telephony.SmsManager 类在 android 1.6 中引入。

添加这些行后,在 1.6 模拟器上显示 toast,但在 1.5 模拟器上它不显示 toast,也没有崩溃。

我原以为应用程序会在 1.5 模拟器上崩溃,但这并没有发生。谁能解释一下?

谢谢

最佳答案

尝试调用不可用类的方法将导致异常。

我不清楚您为什么没有观察到崩溃。也许您有其他代码可以节省您的时间。

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.versions"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MinSdk"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="3" />

</manifest>

Activity 代码:

package com.example.versions;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;

public class MinSdk extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

SmsManager sm = SmsManager.getDefault();
ArrayList<String> stringArray = sm.divideMessage("this is message");
Toast.makeText(this, stringArray.get(0), Toast.LENGTH_LONG).show();
}
}

在 Android 1.5 模拟器中运行时的 Logcat 转储:

E/dalvikvm(  779): Could not find method android.telephony.SmsManager.getDefault, referenced from method com.example.versions.MinSdk.onCreate
W/dalvikvm( 779): VFY: unable to resolve static method 3: Landroid/telephony/SmsManager;.getDefault ()Landroid/telephony/SmsManager;
W/dalvikvm( 779): VFY: rejecting opcode 0x71 at 0x0008
W/dalvikvm( 779): VFY: rejected Lcom/example/versions/MinSdk;.onCreate (Landroid/os/Bundle;)V
W/dalvikvm( 779): Verifier rejected class Lcom/example/versions/MinSdk;
W/dalvikvm( 779): Class init failed in newInstance call (Lcom/example/versions/MinSdk;)
D/AndroidRuntime( 779): Shutting down VM
W/dalvikvm( 779): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
E/AndroidRuntime( 779): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 779): java.lang.VerifyError: com.example.versions.MinSdk
E/AndroidRuntime( 779): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 779): at java.lang.Class.newInstance(Class.java:1472)
E/AndroidRuntime( 779): at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
E/AndroidRuntime( 779): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2186)
E/AndroidRuntime( 779): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
E/AndroidRuntime( 779): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
E/AndroidRuntime( 779): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
E/AndroidRuntime( 779): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 779): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 779): at android.app.ActivityThread.main(ActivityThread.java:3948)
E/AndroidRuntime( 779): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 779): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
E/AndroidRuntime( 779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
E/AndroidRuntime( 779): at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm( 541): GC freed 2 objects / 48 bytes in 194ms
I/Process ( 575): Sending signal. PID: 779 SIG: 3
I/dalvikvm( 779): threadid=7: reacting to signal 3
I/dalvikvm( 779): Wrote stack trace to '/data/anr/traces.txt'

关于Android minSdkVersion 和使用更高级别 SDK 中引入的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3274294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com