gpt4 book ai didi

java - onClick 监听器不起作用 - 无法到达断点

转载 作者:行者123 更新时间:2023-11-29 08:56:17 25 4
gpt4 key购买 nike

我正在尝试设置一个 onclick 监听器,以在单击按钮后更改按钮的背景。我的问题是当我在它的 onClick 方法上设置断点时,使用调试器无法访问它。我相信这可能是一个语法问题——但我已经检查了好几次,但似乎无法找到解决方法。

非常感谢任何帮助。

提前致谢

Java新手

源代码 fragment :

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) {

}

}

}

XML:

<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 &apos;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

最佳答案

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


编辑:

A single button I'm trying to toggle on and off

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

希望这对你有帮助。

关于java - onClick 监听器不起作用 - 无法到达断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20151741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com