如何确保较旧的 API(第 7 级)可以使用较新的功能(API 级别 12)?我希望使用一个 RadioButtons 块。我在 gridLayout 中执行此操作。我使用 Eclipse Juno 进行编辑,Java 编译器 JDK 合规级别设置为 1.6。
在保存包含带有 RadioButtons 的 gridLayout 的布局后,它警告说这在 API 级别 7 中不可用,因此询问我是否要解决我回答是的问题。
我检查了该类型(正确吗?)在布局文件中称为 android.support.v7.widget.GridLayout 而不是使用 GridLayout。带有空 src 子目录的节点“gridlayout_v7”被放置在项目 bij Eclipse 的根目录中。
Android 支持库 v.11 已安装。
但是,当我在虚拟设备(Android 2.1 设备)中运行程序时,LogCat 返回以下错误和警告:
09-16 08:21:44.068: E/AndroidRuntime(678): ERROR: thread attach failed
09-16 08:21:44.894: W/ResourceType(412): Resources don't contain package for resource number 0x7f0700e5
09-16 08:21:44.906: W/ResourceType(412): Resources don't contain package for resource number 0x7f020031
09-16 08:21:44.906: W/ResourceType(412): Resources don't contain package for resource number 0x7f020030
09-16 08:21:44.906: W/ResourceType(412): Resources don't contain package for resource number 0x7f050000
09-16 08:21:44.984: W/ResourceType(412): Resources don't contain package for resource number 0x7f060000
09-16 08:21:44.994: W/ResourceType(412): Resources don't contain package for resource number 0x7f060001
09-16 08:21:45.234: W/ResourceType(412): Resources don't contain package for resource number 0x7f0700e5
09-16 08:21:45.234: W/ResourceType(412): Resources don't contain package for resource number 0x7f020031
09-16 08:21:45.234: W/ResourceType(412): Resources don't contain package for resource number 0x7f020030
09-16 08:21:45.234: W/ResourceType(412): Resources don't contain package for resource number 0x7f050000
09-16 08:21:45.244: W/ResourceType(412): Resources don't contain package for resource number 0x7f060000
09-16 08:21:45.254: W/ResourceType(412): Resources don't contain package for resource number 0x7f060001
09-16 08:21:47.374: E/AndroidRuntime(689): ERROR: thread attach failed
09-16 08:21:47.844: W/dalvikvm(695): VFY: unable to resolve virtual method 2883: Landroid/support/v7/widget/ViewGroup;.onChildVisibilityChanged (Landroid/view/View;II)V
09-16 08:21:47.844: W/dalvikvm(695): VFY: unable to resolve virtual method 2885: Landroid/support/v7/widget/ViewGroup;.onInitializeAccessibilityEvent (Landroid/view/accessibility/AccessibilityEvent;)V
09-16 08:21:47.854: W/dalvikvm(695): VFY: unable to find class referenced in signature (Landroid/view/accessibility/AccessibilityNodeInfo;)
09-16 08:21:47.854: W/dalvikvm(695): VFY: unable to resolve virtual method 2886: Landroid/support/v7/widget/ViewGroup;.onInitializeAccessibilityNodeInfo (Landroid/view/accessibility/AccessibilityNodeInfo;)V
09-16 08:21:47.874: W/dalvikvm(695): VFY: unable to resolve virtual method 2887: Landroid/support/v7/widget/ViewGroup;.onSetLayoutParams (Landroid/view/View;Landroid/view/ViewGroup$LayoutParams;)V
09-16 08:21:47.874: W/dalvikvm(695): VFY: unable to resolve virtual method 2888: Landroid/support/v7/widget/ViewGroup;.onViewAdded (Landroid/view/View;)V
09-16 08:21:47.874: W/dalvikvm(695): VFY: unable to resolve virtual method 2889: Landroid/support/v7/widget/ViewGroup;.onViewRemoved (Landroid/view/View;)V
09-16 08:21:47.884: W/dalvikvm(695): VFY: unable to resolve static method 3016: Landroid/view/View;.resolveSizeAndState (III)I
错误日志显示没有错误。
控制台显示以下(无错误):
[2012-11-20 16:59:22 - SimpleScore] ------------------------------
[2012-11-20 16:59:22 - SimpleScore] Android Launch!
[2012-11-20 16:59:22 - SimpleScore] adb is running normally.
[2012-11-20 16:59:22 - SimpleScore] Performing nl.computerhuys.simplescore.MainActivity activity launch
[2012-11-20 16:59:22 - SimpleScore] Automatic Target Mode: launching new emulator with compatible AVD 'VirtualAndroid2.1'
[2012-11-20 16:59:22 - SimpleScore] Launching a new emulator with Virtual Device 'VirtualAndroid2.1'
[2012-11-20 16:59:32 - Emulator] Warning: No DNS servers found
[2012-11-20 16:59:33 - SimpleScore] New emulator found: emulator-5554
[2012-11-20 16:59:33 - SimpleScore] Waiting for HOME ('android.process.acore') to be launched...
[2012-11-20 17:00:20 - SimpleScore] HOME is up on device 'emulator-5554'
[2012-11-20 17:00:20 - SimpleScore] Uploading SimpleScore.apk onto device 'emulator-5554'
[2012-11-20 17:00:22 - SimpleScore] Installing SimpleScore.apk...
[2012-11-20 17:00:29 - SimpleScore] Success!
[2012-11-20 17:00:29 - SimpleScore] Starting activity nl.computerhuys.simplescore.MainActivity on device emulator-5554
[2012-11-20 17:00:32 - SimpleScore] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=nl.computerhuys.simplescore/.MainActivity }
源代码文件 MainActivity.java 的完整代码:
package nl.computerhuys.simplescore;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
布局文件activity_main.xml的完整代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/nl.computerhuys.simplescore"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="133dp"
android:text="@string/speler1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="33dp"
android:text="@string/speler2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="33dp"
android:text="@string/speler3"
android:textAppearance="?android:attr/textAppearanceLarge" />
<android.support.v7.widget.GridLayout
android:id="@+id/gridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_marginLeft="19dp"
android:layout_marginTop="-5dp"
android:layout_toRightOf="@+id/textView2" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="0"
app:layout_gravity="right"
app:layout_row="0"
android:text="@string/score1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="1"
app:layout_gravity="left"
app:layout_row="0"
android:text="@string/score2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="2"
app:layout_row="0"
android:text="@string/score3" />
<android.support.v7.widget.Space
android:layout_width="100dp"
android:layout_height="28dp"
app:layout_column="0"
app:layout_row="0" />
</android.support.v7.widget.GridLayout>
<android.support.v7.widget.GridLayout
android:id="@+id/gridLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/textView2"
android:layout_marginLeft="19dp"
android:layout_marginTop="-5dp"
android:layout_toRightOf="@id/textView2" >
<RadioButton
android:id="@+id/RadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="0"
app:layout_gravity="right"
app:layout_row="0"
android:text="@string/score1" />
<RadioButton
android:id="@+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="1"
app:layout_gravity="left"
app:layout_row="0"
android:text="@string/score2" />
<RadioButton
android:id="@+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="2"
app:layout_gravity="left"
app:layout_row="0"
android:text="@string/score3" />
<android.support.v7.widget.Space
android:layout_width="100dp"
android:layout_height="28dp"
app:layout_column="0"
app:layout_row="0" />
</android.support.v7.widget.GridLayout>
<android.support.v7.widget.GridLayout
android:id="@+id/gridLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView3"
android:layout_marginLeft="19dp"
android:layout_marginTop="-5dp"
android:layout_toRightOf="@id/textView2" >
<RadioButton
android:id="@+id/RadioButton05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="0"
app:layout_gravity="right"
app:layout_row="0"
android:text="@string/score1" />
<RadioButton
android:id="@+id/RadioButton06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="1"
app:layout_gravity="left"
app:layout_row="0"
android:text="@string/score2" />
<RadioButton
android:id="@+id/RadioButton04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="2"
app:layout_gravity="left"
app:layout_row="0"
android:text="@string/score3" />
<android.support.v7.widget.Space
android:layout_width="100dp"
android:layout_height="28dp"
app:layout_column="0"
app:layout_row="0" />
</android.support.v7.widget.GridLayout>
</RelativeLayout>
最后,这是清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.computerhuys.simplescore"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
单击虚拟设备中的 RadioButton(在第一次强制应用程序显示之后)会导致以下 LogCat 错误:
09-16 09:05:04.056: W/KeyCharacterMap(412): No keyboard for id 0
09-16 09:05:04.064: W/KeyCharacterMap(412): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-16 09:05:15.075: W/ActivityManager(412): Scheduling restart of crashed service com.android.inputmethod.latin/.LatinIME in 5000ms
09-16 09:05:15.095: E/ActivityThread(412): Failed to find provider info for android.server.checkin
09-16 09:05:15.105: W/Checkin(412): Can't log event SYSTEM_SERVICE_LOOPING: java.lang.IllegalArgumentException: Unknown URL content://android.server.checkin/events
09-16 09:05:21.114: W/InputMethodManager(695): Ignoring onBind: cur seq=-1, given seq=1
选择单选按钮后,无法通过再次单击或选择同一组中的另一个单选按钮(相同的 GridLayout)来取消选择它。
什么问题,如何解决?