2

小さな小さなアプリを作成していますが、「... MockView を android.view.ViewGroup にキャストできません」というエラーが表示され、ディスプレイが設定されていません。選択時に状態とテキストを切り替えるボタンを作ろうとしています。どんな助けでも大歓迎です。

メインクラス:

package rechee.pro.sound;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class PrometheusSoundActivity extends Activity implements OnClickListener {

    ImageButton pressed;
    ImageButton not_pressed;

    private MediaPlayer mp;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setVolumeControlStream(AudioManager.STREAM_MUSIC);

         ImageButton pressed = (ImageButton) findViewById(R.id.pressed);
        pressed.setOnClickListener(this);

         ImageButton not_pressed= (ImageButton) findViewById(R.id.not_pressed);
        not_pressed.setOnClickListener(this);

       // View clickButton= findViewById(R.id.click_button);
       //clickButton.setOnClickListener(this);
    }
    int resId;
    public void onClick(View v) {
        if(pressed.isSelected()){
            resId= R.raw.sound;
            mp= MediaPlayer.create(this, resId);
            mp.start();



        }
        if(not_pressed.isSelected()){
            mp.stop();
        }

        if(mp!=null){
            mp.release();
        }



    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="@drawable/promethus">
        <item android:id="@+id/not_pressed"
            android:state_pressed="false"
            android:text="@string/Click"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <item android:id="@+id/pressed"
            android:state_pressed="true"
            android:text="@string/stop_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        </selector>




</LinearLayout>

文字列 xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, PrometheusSoundActivity!</string>
    <string name="Click">Click here</string><string name="stop_button">Stop</string><string name="app_name">Prometheus Sound</string>



</resources>
4

1 に答える 1

0

そのような線形レイアウト内にセレクターを配置できるかどうか、または android:id がセレクター項目内に含める有効なタグであるかどうかはわかりません (参照: http://developer.android.com/guide/topics /resources/drawable-resource.html#StateList )。押されたときに状態が変化するボタンが必要な場合は、ToggleButton を使用して目的を達成するか (http://developer.android.com/reference/android/widget/ToggleButton.html)、プログラムで設定することができます。ボタンを押したときにテキストを変更します。トグル ボタンを使用する場合は、xml で ToggleButton として宣言します。

<ToggleButton
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textOff="textThatShowsWhenButtonIsOff"
    android:textOn="textThatShowsWhenButtonIsPressed" />

と:

public void onClick(View arg0) {
    ToggleButton button = (ToggleButton) arg0;
    if (button.isChecked()) {
        Log.d("MainActivity", "Button is now checked");
    } else {
        Log.d("MainActivity", "Button is now off");
    }

}
于 2012-07-29T04:03:21.603 に答える