Android 2.1 SDK から特定のコードサンプルを (少しずつ) コピー (およびわずかに変更) して、Android 2.1 / Java アプリのプログラミングを開始しようとしています: C:\Program Files\Java\android-2.1_r03-windows \samples\android-7\ApiDemos\src\com\example\android\apis\view\RadioGroup1.java
RadioButtons を選択することができ、その結果、グループ内の他のラジオボタンは選択解除されます。ここまでは順調ですね。しかし、元のプログラムとは異なり、クリア ('C') をクリックしても何も起こらず、'選択された' TextView には選択された ID が表示されません。最初のテキスト「You have selected: (none)」(「C:-」に変更) が表示され、ラジオボタンの 1 つをクリックしても更新されません)。
誰が何が悪いのか/どこに進むべきか教えてもらえますか?
また、プロセスでいくつかの .xml ファイルを変更/部分的にコピー/追加しました: AndroidManifest.xml、layout/activity_main.xml、values/drawables.xml、values/ids.xml、values/strings.xml、values/styles.xml、 drawable/filled_box.xml (および drawable ディレクトリにいくつかの .png ファイルを追加)。android-support-v4.jar は、下位互換性のためにプロジェクトの lib ディレクトリにあります (ターゲット 15、最小 7)。
RadioGroup1.java は次のとおりです。
package nl.computerhuys.simplescore;
import nl.computerhuys.simplescore.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.RadioGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.LinearLayout;
public class RadioGroup1 extends Activity implements RadioGroup.OnCheckedChangeListener,
View.OnClickListener {
private TextView mChoice;
private RadioGroup mRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRadioGroup = (RadioGroup) findViewById(R.id.menu);
// test adding a radio button programmatically
RadioButton newRadioButton = new RadioButton(this);
newRadioButton.setText(R.string.radio_group_s1);
newRadioButton.setId(R.id.s1);
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
mRadioGroup.addView(newRadioButton, 0, layoutParams);
// test listening to checked change events
String selection = getString(R.string.radio_group_selection);
mRadioGroup.setOnCheckedChangeListener(this);
mChoice = (TextView) findViewById(R.id.choice);
mChoice.setText(selection + mRadioGroup.getCheckedRadioButtonId());
// test clearing the selection
Button clearButton = (Button) findViewById(R.id.clear);
clearButton.setOnClickListener(this);
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
String selection = getString(R.string.radio_group_selection);
String none = getString(R.string.radio_group_none);
mChoice.setText(selection +
(checkedId == View.NO_ID ? none : checkedId));
}
public void onClick(View v) {
mRadioGroup.clearCheck();
}
}
MainActivity.java は次のとおりです。
package nl.computerhuys.simplescore;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
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:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android1:background="@drawable/screen_background_black"
tools:context=".MainActivity" >
<RadioGroup
android:id="@+id/menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/s1"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/s1"
android:text="@string/radio_group_1_s1" />
<RadioButton
android:id="@+id/s2"
android:text="@string/radio_group_1_s2" />
<RadioButton
android:id="@+id/s3"
android:text="@string/radio_group_1_s3" />
<TextView
android:id="@+id/choice"
android:text="@string/radio_group_1_selection" />
<Button
android1:id="@+id/clear"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:text="@string/radio_group_1_clear" />
</RadioGroup>
</RelativeLayout>
および AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="nl.computerhuys.mylibrary" android:required="false" />
<activity
android:name="nl.computerhuys.simplescore.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="view.RadioGroup1" android:label="Views/Radio Group" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
</application>
</manifest>
変更後のlogCatファイル(元の投稿を編集する以外に投稿する方法がわかりません:(
09-16 16:18:01.377: D/AndroidRuntime(1476): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
09-16 16:18:01.377: D/AndroidRuntime(1476): CheckJNI is ON
09-16 16:18:01.884: D/AndroidRuntime(1476): --- registering native functions ---
09-16 16:18:02.714: D/ddm-heap(1476): Got feature list request
09-16 16:18:03.804: D/PackageParser(412): Scanning package: /data/app/vmdl47668.tmp
09-16 16:18:04.514: I/PackageManager(412): Removing non-system package:nl.computerhuys.simplescore
09-16 16:18:04.524: D/PackageManager(412): Removing package nl.computerhuys.simplescore
09-16 16:18:04.534: D/PackageManager(412): Activities: nl.computerhuys.simplescore.MainActivity
09-16 16:18:04.784: D/PackageManager(412): Scanning package nl.computerhuys.simplescore
09-16 16:18:04.795: I/PackageManager(412): /data/app/vmdl47668.tmp changed; unpacking
09-16 16:18:04.826: D/installd(31): DexInv: --- BEGIN '/data/app/vmdl47668.tmp' ---
09-16 16:18:07.805: D/dalvikvm(1482): DexOpt: load 365ms, verify 1635ms, opt 71ms
09-16 16:18:08.005: D/installd(31): DexInv: --- END '/data/app/vmdl47668.tmp' (success) ---
09-16 16:18:08.005: D/PackageManager(412): Activities: nl.computerhuys.simplescore.MainActivity
09-16 16:18:08.025: D/ActivityManager(412): Uninstalling process nl.computerhuys.simplescore
09-16 16:18:08.035: D/ActivityManager(412): Force removing process ProcessRecord{44f15ef0 1468:nl.computerhuys.simplescore/10028} (nl.computerhuys.simplescore/10028)
09-16 16:18:08.055: I/Process(412): Sending signal. PID: 1468 SIG: 9
09-16 16:18:08.195: I/UsageStats(412): Unexpected resume of com.android.launcher while already resumed in nl.computerhuys.simplescore
09-16 16:18:08.295: I/WindowManager(412): WIN DEATH: Window{44d60a30 nl.computerhuys.simplescore/nl.computerhuys.simplescore.MainActivity paused=false}
09-16 16:18:08.435: D/ActivityManager(412): Received spurious death notification for thread android.os.BinderProxy@44d89e10
09-16 16:18:08.795: W/InputManagerService(412): Got RemoteException sending setActive(false) notification to pid 1468 uid 10028
09-16 16:18:09.787: I/installd(31): move /data/dalvik-cache/data@app@vmdl47668.tmp@classes.dex -> /data/dalvik-cache/data@app@nl.computerhuys.simplescore.apk@classes.dex
09-16 16:18:09.825: D/PackageManager(412): New package installed in /data/app/nl.computerhuys.simplescore.apk
09-16 16:18:10.075: D/AndroidRuntime(1476): Shutting down VM
09-16 16:18:10.085: D/dalvikvm(1476): DestroyJavaVM waiting for non-daemon threads to exit
09-16 16:18:10.104: D/dalvikvm(1476): DestroyJavaVM shutting VM down
09-16 16:18:10.104: D/dalvikvm(1476): HeapWorker thread shutting down
09-16 16:18:10.104: D/dalvikvm(1476): HeapWorker thread has shut down
09-16 16:18:10.104: D/jdwp(1476): JDWP shutting down net...
09-16 16:18:10.115: I/dalvikvm(1476): Debugger has detached; object registry had 1 entries
09-16 16:18:10.128: D/dalvikvm(1476): VM cleaning up
09-16 16:18:10.155: E/AndroidRuntime(1476): ERROR: thread attach failed
09-16 16:18:10.187: D/ActivityManager(412): Uninstalling process nl.computerhuys.simplescore
09-16 16:18:10.225: D/dalvikvm(1476): LinearAlloc 0x0 used 623916 of 5242880 (11%)
09-16 16:18:11.376: D/dalvikvm(412): GC freed 9687 objects / 641216 bytes in 666ms
09-16 16:18:11.414: W/ResourceType(412): Resources don't contain package for resource number 0x7f0700e5
09-16 16:18:11.425: W/ResourceType(412): Resources don't contain package for resource number 0x7f020031
09-16 16:18:11.425: W/ResourceType(412): Resources don't contain package for resource number 0x7f020030
09-16 16:18:11.425: W/ResourceType(412): Resources don't contain package for resource number 0x7f050000
09-16 16:18:11.725: W/ResourceType(412): Resources don't contain package for resource number 0x7f060000
09-16 16:18:11.745: W/ResourceType(412): Resources don't contain package for resource number 0x7f060001
09-16 16:18:12.217: D/dalvikvm(458): GC freed 149 objects / 5952 bytes in 756ms
09-16 16:18:12.276: W/ResourceType(412): Resources don't contain package for resource number 0x7f0700e5
09-16 16:18:12.276: W/ResourceType(412): Resources don't contain package for resource number 0x7f020031
09-16 16:18:12.276: W/ResourceType(412): Resources don't contain package for resource number 0x7f020030
09-16 16:18:12.276: W/ResourceType(412): Resources don't contain package for resource number 0x7f050000
09-16 16:18:12.475: W/ResourceType(412): Resources don't contain package for resource number 0x7f060000
09-16 16:18:12.485: W/ResourceType(412): Resources don't contain package for resource number 0x7f060001
09-16 16:18:12.894: D/AndroidRuntime(1487): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
09-16 16:18:12.904: D/AndroidRuntime(1487): CheckJNI is ON
09-16 16:18:14.004: D/AndroidRuntime(1487): --- registering native functions ---
09-16 16:18:14.924: D/ddm-heap(1487): Got feature list request
09-16 16:18:15.854: I/ActivityManager(412): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=nl.computerhuys.simplescore/.MainActivity }
09-16 16:18:15.944: D/AndroidRuntime(1487): Shutting down VM
09-16 16:18:15.944: D/dalvikvm(1487): DestroyJavaVM waiting for non-daemon threads to exit
09-16 16:18:15.954: D/dalvikvm(1487): DestroyJavaVM shutting VM down
09-16 16:18:15.964: D/dalvikvm(1487): HeapWorker thread shutting down
09-16 16:18:15.964: D/dalvikvm(1487): HeapWorker thread has shut down
09-16 16:18:15.964: D/jdwp(1487): JDWP shutting down net...
09-16 16:18:15.964: I/dalvikvm(1487): Debugger has detached; object registry had 1 entries
09-16 16:18:15.974: D/dalvikvm(1487): VM cleaning up
09-16 16:18:16.044: E/AndroidRuntime(1487): ERROR: thread attach failed
09-16 16:18:16.134: D/dalvikvm(1487): LinearAlloc 0x0 used 639500 of 5242880 (12%)
09-16 16:18:16.644: I/ActivityManager(412): Start proc nl.computerhuys.simplescore for activity nl.computerhuys.simplescore/.MainActivity: pid=1494 uid=10028 gids={}
09-16 16:18:17.224: D/ddm-heap(1494): Got feature list request
09-16 16:18:18.604: I/ActivityManager(412): Displayed activity nl.computerhuys.simplescore/.MainActivity: 2599 ms (total 2599 ms)
09-16 16:18:23.915: D/dalvikvm(458): GC freed 2339 objects / 135960 bytes in 262ms
09-16 16:18:29.025: D/dalvikvm(568): GC freed 43 objects / 2016 bytes in 307ms
変更後の MainActivity.java:
package nl.computerhuys.simplescore;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import nl.computerhuys.simplescore.R;
public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener,
View.OnClickListener {
private TextView mChoice;
private RadioGroup mRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRadioGroup = (RadioGroup) findViewById(R.id.menu);
// test adding a radio button programmatically
RadioButton newRadioButton = new RadioButton(this);
newRadioButton.setText(R.string.radio_group_s1);
newRadioButton.setId(R.id.s1);
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
mRadioGroup.addView(newRadioButton, 0, layoutParams);
// test listening to checked change events
String selection = getString(R.string.radio_group_selection);
mRadioGroup.setOnCheckedChangeListener(this);
mChoice = (TextView) findViewById(R.id.choice);
mChoice.setText(selection + mRadioGroup.getCheckedRadioButtonId());
// test clearing the selection
Button clearButton = (Button) findViewById(R.id.clear);
clearButton.setOnClickListener(this);
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
String selection = getString(R.string.radio_group_selection);
String none = getString(R.string.radio_group_none);
mChoice.setText(selection +
(checkedId == View.NO_ID ? none : checkedId));
}
public void onClick(View v) {
mRadioGroup.clearCheck();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
変更後のマニフェスト:
<?xml version="1.0" encoding="utf-8"?>
<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="16" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:name="nl.computerhuys.simplescore.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
変更後の Activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/screen_background_black" >
<RadioGroup
android:id="@+id/menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/s1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_group_1_s1" />
<RadioButton
android:id="@+id/s2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_group_1_s2" />
<RadioButton
android:id="@+id/s3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_group_1_s3" />
<TextView
android:id="@+id/choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_group_1_selection" />
</RadioGroup>
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/menu"
android:text="@string/radio_group_1_clear" />
</RelativeLayout>