java - MockView 无法转换为 android.view.ViewGroup

标签 java android

我正在创建一个小应用程序,但出现错误“...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>

最佳答案

我不确定您是否可以将选择器放入这样的线性布局中,或者 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");
    }

}

关于java - MockView 无法转换为 android.view.ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11706354/

相关文章:

java - 出现 NumberFormatException 和 parseInt 错误

java - 解析所有 ISO 8601 DateTime 格式 Java 1.8

java - 添加两个对象——难以理解接受一个对象作为参数

使用 Intent 不打开应用程序的 Android 深层链接

android - 如何解析来自 URL 的 XML 数据到 Google Map V2?

android - 我们如何解决 PagerTitleStrip 和 PagerTabStrip 中的空白标题?

java - 打开卡片 View 时如何获取特定用户的数据

Java Swing : Separation of logic and GUI

java - 即使将 PrintStrem 设置为 UTF-8,也无法打印 unicode 字符

android - 使用 Dagger 注入(inject) RecyclerView.Adapter