我正在尝试设置onclick侦听器,以便在单击按钮后更改按钮的背景。我的问题是当我在它的onClick方法上设置一个断点时,使用调试器无法访问它。我相信这可能是一个语法问题 - 但我已经多次查看它,似乎无法发现如何解决它。
非常感谢任何帮助。
提前致谢,
JavaNoob
public void onClick(View v) {
if (v == fav_up_btn) {
fav_up_btn.setBackgroundResource(R.drawable.fav_dwn_btn);
}
}
public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {
public static final String API_KEY = "AIzaSyC0Te2pyooXzuyLaE6_SsFlITKCwjj55fI";
public static final String VIDEO_ID = "o7VVHhK9zf0";
public static final String VIDEO1_ID = "xVHHJqntuXI";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
private YouTubeThumbnailView youTubeThumbnailView1;
private YouTubeThumbnailView youTubeThumbnailView2;
private YouTubeThumbnailView youTubeThumbnailView3;
private YouTubeThumbnailView youTubeThumbnailView4;
private YouTubeThumbnailLoader youTubeThumbnailLoader;
ScrollView mainScrollView;
Button fav_up_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);
youTubeThumbnailView1 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview1);
youTubeThumbnailView1.initialize(API_KEY, this);
fav_up_btn = (Button)findViewById(R.id.fav_up_btn);
fav_up_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (v == fav_up_btn) {
fav_up_btn.setBackgroundResource(R.drawable.fav_dwn_btn);
}
}
});
youTubeThumbnailView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO1_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
youTubeThumbnailView2 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview2);
youTubeThumbnailView2.initialize(API_KEY, this);
youTubeThumbnailView2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg1) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO1_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
youTubeThumbnailView3 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview3);
youTubeThumbnailView3.initialize(API_KEY, this);
youTubeThumbnailView3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg1) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO1_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
youTubeThumbnailView4 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview4);
youTubeThumbnailView4.initialize(API_KEY, this);
youTubeThumbnailView4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg1) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO1_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
}
@Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
youTubePlayer = player;
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
@Override
public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
YouTubeInitializationResult error) {
}
@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
youTubeThumbnailLoader = thumbnailLoader;
thumbnailLoader
.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader.setVideo(VIDEO_ID);
}
private final class ThumbnailLoadedListener implements
YouTubeThumbnailLoader.OnThumbnailLoadedListener {
@Override
public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:textStyle="bold" />
<ScrollView
android:id="@+id/groupScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtubeplayerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1a"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Throw 'Em Up"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/fav_up_btn"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1a"
android:background="@drawable/fav_up_btn"
android:gravity="right"
android:paddingRight="5dp"
android:paddingTop="5dp" />
<selector>
<item
android:drawable="@drawable/fav_up_btn"
android:state_focused="true"
android:state_pressed="false"/>
<item
android:drawable="@drawable/fav_dwn_btn"
android:state_focused="true"
android:state_pressed="true"/>
<item
android:drawable="@drawable/fav_dwn_btn"
android:state_focused="false"
android:state_pressed="true"/>
<item android:drawable="@drawable/fav_up_btn"/>
</selector>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView3a"
android:gravity="left"
android:text="by DJ Generic |"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightgrey" />
<TextView
android:id="@+id/textView3a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text=" 100,000 views"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="25dp" >
</View>
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumbnailview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bulls On Parade"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/fav_up_btn"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1a"
android:background="@drawable/fav_up_btn"
android:gravity="right"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView3a"
android:gravity="left"
android:text="by Rage Against The Machine |"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightgrey" />
<TextView
android:id="@+id/textView3a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text=" 100,000 views"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="25dp" >
</View>
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumbnailview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Isaac Daniel on CNN with Anderson Cooper"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/fav_up_btn"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1a"
android:background="@drawable/fav_up_btn"
android:gravity="right"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView3a"
android:gravity="left"
android:text="by idconex |"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightgrey" />
<TextView
android:id="@+id/textView3a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView2a"
android:layout_weight="1"
android:gravity="left"
android:text=" 100,000 views"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="25dp" >
</View>
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumbnailview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bulls On Parade"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/fav_up_btn"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1a"
android:background="@drawable/fav_up_btn"
android:gravity="right"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView3a"
android:gravity="left"
android:text="by Rage Against The Machine |"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightgrey" />
<TextView
android:id="@+id/textView3a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text=" 100,000 views"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="25dp" >
</View>
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumbnailview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Isaac Daniel on CNN with Anderson Cooper"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/fav_up_btn"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1a"
android:background="@drawable/fav_up_btn"
android:gravity="right"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView3a"
android:gravity="left"
android:text="by idconex |"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightgrey" />
<TextView
android:id="@+id/textView3a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text=" 100,000 views"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="5dp" >
</View>
</LinearLayout>
</ScrollView>
</LinearLayout>
11-22 13:46:00.015: E/AndroidRuntime(21880): FATAL EXCEPTION: main
11-22 13:46:00.015: E/AndroidRuntime(21880): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.idg2/com.idg.omv.Home}: android.view.InflateException: Binary XML file line #56: Error inflating class selector
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.app.ActivityThread.access$700(ActivityThread.java:140)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.os.Looper.loop(Looper.java:137)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.app.ActivityThread.main(ActivityThread.java:4921)
11-22 13:46:00.015: E/AndroidRuntime(21880): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 13:46:00.015: E/AndroidRuntime(21880): at java.lang.reflect.Method.invoke(Method.java:511)
11-22 13:46:00.015: E/AndroidRuntime(21880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
11-22 13:46:00.015: E/AndroidRuntime(21880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
11-22 13:46:00.015: E/AndroidRuntime(21880): at dalvik.system.NativeStart.main(Native Method)
11-22 13:46:00.015: E/AndroidRuntime(21880): Caused by: android.view.InflateException: Binary XML file line #56: Error inflating class selector
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-22 13:46:00.015: E/AndroidRuntime(21880): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.app.Activity.setContentView(Activity.java:1924)
11-22 13:46:00.015: E/AndroidRuntime(21880): at com.idg.omv.Home.onCreate(Home.java:43)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.app.Activity.performCreate(Activity.java:5206)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
11-22 13:46:00.015: E/AndroidRuntime(21880): ... 11 more
11-22 13:46:00.015: E/AndroidRuntime(21880): Caused by: java.lang.ClassNotFoundException: android.view.selector
11-22 13:46:00.015: E/AndroidRuntime(21880): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-22 13:46:00.015: E/AndroidRuntime(21880): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-22 13:46:00.015: E/AndroidRuntime(21880): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
11-22 13:46:00.015: E/AndroidRuntime(21880): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
11-22 13:46:00.015: E/AndroidRuntime(21880): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
11-22 13:46:00.015: E/AndroidRuntime(21880): ... 24 more
11-22 13:46:11.205: I/Process(21880): Sending signal. PID: 21880 SIG: 9
答案 0 :(得分:0)
如果您在xml中定义了按钮,请在onCreate()中的某处添加代码:
fav_up_btn = (Button)findViewById(R.id.fav_up_btn);
然后设置您的点击监听器。如果您没有xml中的按钮,并且想要在更新的代码中创建它(fav_up_btn = new Button(this)),则需要稍后将其添加到布局中。如果您在xml中定义了布局,请为其添加一个ID,如果它不存在:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
然后,要添加按钮,您可以这样做:
LinearLayout layout = (LinearLayout) findViewById(R.id.myLayout);
layout.addView(fav_up_btn);
或者像那样(现在你在代码中创建按钮的方式):
LinearLayout layout = (LinearLayout) findViewById(R.id.myLayout);
Button fav_up_btn = new Button(this);
fav_up_btn.setText(roomName);
fav_up_btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(fav_up_btn);
在任何一种情况下都要记住设置点击监听器。
答案 1 :(得分:0)
fav_up_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (v == fav_up_btn) {
fav_up_btn.setBackgroundResource(R.drawable.fav_dwn_btn);
}
}
});
为什么你在监听器中有条件,如果你对其他按钮使用相同的监听器则会有意义。 但在你的情况下,我不明白为什么你需要它。
试试这样,检查你的logcat:
fav_up_btn = (Button)findViewById(R.id.fav_up_btn);
fav_up_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fav_up_btn.setBackgroundResource(R.drawable.fav_dwn_btn);
Log.d("mylog","fav_up_btn is clicked");
}
});
<小时/> 编辑:
我正试图打开和关闭的单个按钮
fav_up_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean favIsUp = fav_up_btn.getBackground().getConstantState()
.equals(getResources().getDrawable(R.drawable.fav_up_btn).getConstantState());
//set the background
fav_up_btn.setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn : R.drawable.fav_up_btn);
}
});
希望这可以帮助你。
答案 2 :(得分:0)
当然问题在于你的OnClickListener
按钮; - )
fav_up_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (v == fav_up_btn) {
fav_up_btn.setBackgroundResource(R.drawable.fav_dwn_btn);
}
}
});
您使用Objects
代替==
equals
方法,将2 Object
相互比较,就像它们是原始类型一样。
要检查是否点击了正确的视图,您应该将视图的ID与xml布局中的id进行比较,如下所示:
if (v.getId() == R.id.fav_up_btn)
此外,检查是否需要,因为您为每个Button
使用匿名侦听器,因此其他按钮不会被此OnClickListener
捕获。
编辑:我看到你已经改变了你的问题(也添加了你的XML布局)。这当然有所不同 - 这部分在XML布局文件中无效:
<selector>
<item
android:drawable="@drawable/fav_up_btn"
android:state_focused="true"
android:state_pressed="false"/>
<item
android:drawable="@drawable/fav_dwn_btn"
android:state_focused="true"
android:state_pressed="true"/>
<item
android:drawable="@drawable/fav_dwn_btn"
android:state_focused="false"
android:state_pressed="true"/>
<item android:drawable="@drawable/fav_up_btn"/>
</selector>
选择器应该驻留在drawable
文件夹中,而应该在按钮的XML中引用它,如下所示:android:background="@drawable/mySelector"
。
第二次编辑:只是为了澄清你看到的异常。启动Activity
时,Android会尝试“膨胀”您的XML布局 - 当它到达<selector>
标记时,它会尝试像XML布局的其余部分那样对其进行充气 - XML布局中的项目是视图或小部件,因此inflater系统崩溃了应用程序,因为它不知道如何处理<selector>
标记,因为它不是视图。