0

我正在尝试通过(逐个)复制(并稍微修改)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

可以选择单选按钮,从而取消选择组中的其他单选按钮。到现在为止还挺好。但与原始程序不同的是,单击 Clear ('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 (并在可绘制目录中添加了一些 .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>
4

2 回答 2

0

你只是在运行MainActivity,而不是RadioGroup1.

任何一个:

  • RadioGroup1通过将所有一个代码剪切并粘贴到MainActivity.
  • 或者保留两个活动startActivity(new Intent(this, RadioGroup1.class));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>

名称不正确,我相信您只能将一个 Activity 设置为MAIN

<activity android:name=".RadioGroup1" android:label="Views/Radio Group" android:exported="false" />
于 2012-11-21T20:50:34.367 回答
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>

您的布局文件中有几个错误:

  • android1 前缀无用且令人困惑
  • 单选组应该只包含单选按钮
  • 每个视图必须同时具有 android:layout_width 和 android:layout_height 属性
  • 工具命名空间和工具:上下文是无用的
  • 请配置您的 IDE 以不使用制表符而是使用空格。*修改缩进非常痛苦。

此外,您的清单中还有一个问题:您必须仅使用意图过滤器声明一个活动来捕获启动器意图。那是您的程序入口点,实际上您声明了两个...

暂时将这两项活动合二为一。不要以编程方式创建新的小部件,首先使用 findViewById 获取您在布局中声明的小部件。此外,您创建的视图的 id 与清单中的一个视图冲突。

如果您决定稍后声明更多活动,请仔细检查您是否在清单中使用了它们的完全限定名称,您在 RadioGroup1 活动声明声明中关于该问题的错误。

于 2012-11-21T20:51:55.197 回答