2012-07-29 104 views
2

我正在创建一个小的小应用程序,但我得到错误“... MockView无法转换为android.view.ViewGroup”,并且没有显示设置。我正在尝试使用一个按钮,可以在选中状态和文本时进行切换。任何帮助将非常感激。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> 

回答

0

我不知道,如果你可以把一个选择那样的线性布局内,或者如果android:id甚至是一个在选择器项目内部有效的标签(请参阅:http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList)。如果你想有一个按钮在按下时改变状态,那么ToggleButton可以自己做你想做的事(http://developer.android.com/reference/android/widget/ToggleButton.html),或者你可以编程设置按钮来改变它的文本。在使用切换按钮的情况下,您需要将其作为ToggleButton在xml中声明。

<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"); 
    } 

} 
+0

你确定你能有一个切换按钮的文字?没有文字出现。 – recheej 2012-07-29 04:37:30

+0

是的 - 文字应该显示。这就是textOff和textOn属性的作用。如果您看不到文字,请检查您的文字颜色和背景,或者只是发布所有来源,我会尽力帮助您。我只是用代码编写了一个示例按钮,它对我来说工作正常。 – aamit915 2012-07-29 04:51:48

+0

我收到一个未处理的循环错误。我会更新当我解决这个问题时会发生什么。 – recheej 2012-07-29 04:56:13