我正在尝试通过(逐段)复制(并稍微修改)Android 2.1 sdk中的特定代码示例来开始编程Android 2.1 / Java应用程序:C:\ Program Files \ Java \ android-2.1 _r03窗口\样品\机器人-7 \ ApiDemos \ SRC \ COM \示例\机器人\的API \视图\ RadioGroup1.java
可以选择RadioButtons,结果取消选择组中的其他radiobuttons。到现在为止还挺好。但与原始程序不同的是,单击“清除”(“C”)不执行任何操作,“选定”TextView不会显示所选的ID。显示初始文本“您已选择:(无)”(修改为“C: - ”),单击其中一个单选按钮后不会更新。
有谁可以说出错了/在哪里继续?
我还在流程中修改/部分复制/添加了几个.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>
答案 0 :(得分:0)
您只运行MainActivity
,而不是RadioGroup1
。
或者:
RadioGroup1
个代码粘贴到MainActivity
。startActivity(new Intent(this, RadioGroup1.class));
的{{1}} MainAcitivity
onCreate()
如果您同时保留这两项活动,则必须调整您的清单。这不太对劲:
<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>
名称不正确,我相信您只能将一个活动设置为MAIN
:
<activity android:name=".RadioGroup1" android:label="Views/Radio Group" android:exported="false" />
答案 1 :(得分:0)
从清单中删除使用库。
将不是RadioButton的所有内容移出ButtonGroup。将它们放在您的组下面并添加xml语句,将它们放在RadioGroup下面的相对布局(根级别)中。
<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:checkedButton="@+id/s1"
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" />
</RadioGroup>
<TextView
android:id="@+id/choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/menu"
android:text="@string/radio_group_1_selection" />
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/choice"
android:text="@string/radio_group_1_clear" />
</RelativeLayout>
布局文件中存在一些错误:
此外,您在yuor清单中遇到问题:您必须使用intent-filter仅声明一个活动来捕获启动器意图。这是你的程序入口点,实际上你宣布了两个......
将这两项活动合二为一。并且不要以编程方式创建新的小部件,首先使用findViewById获取您在布局中声明的小部件。此外,您创建的视图的ID与清单中的一个视图相冲突。
如果您决定稍后宣布更多活动,请仔细检查您是否在清单中使用其完全限定名称,而您在RadioGroup1活动声明声明中是错误的。