android 重写view重写onClick方法出错,程序运行不了。

(已有0条评论)
你还可以输入200字
  编辑推荐
Copyright (c) 2012
All Rights Reserved. 琵琶网风云天下OL专区版权所有.
抵制不良游戏 拒绝盗版游戏 注意自我保护 谨防受骗上当 适度游戏益脑 沉迷游戏伤身 合理安排时间
享受健康生活评论-2117&
trackbacks-0
1) & & & &在实现从1个activity跳转到另一个activity时出现如下错误:
    Description & &Resource Path Location&& Type
    The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (HelloworldActivity.MyButtonListener)&         &     &&HelloworldActivity.java&&& /helloworld/src/com/example/helloworld&&&&&& line 19&&&&& Java Problem
    则说明是导入的android包弄错了,我们应该把
    import &android.content.DialogInterface.OnClickL改为
    import android.view.V//注意view的大小写
    import android.view.View.OnClickL
2)&&&&&&&& 出现Unfortunately,YourPorjectName has stopped.类似的错误,则有可能是在该activity对应的xml文件中,对应某个控件的属性没有设置好,导致不能显示,所以会出现这种错误。
3)&&&&&&&& 如果出现Unparsed aapt error(s)!Check the console for output.则有可能是资源文件命名出现错误,比如说命名时出现了空格。我遇到的是图片的命名中出现了空格就会有该错误提      示。
4)&&&&&&&& android开发中,在导入一个已经存在的项目出现如下错误:invalid project description时,则需要把目录下的.project中的&name&XXXX&/name&中的XXX该成和目录文件夹名一样,      &因为有可能是你把文件夹的名字改了而导致的。
5)    如果出现如下错误:Description Resource Path Location Type
     The type MainActivity.RealTimeDataListener must implement the inherited abstract method Animation.AnimationListener.onAnimationEnd(Animation)           MainActivity.java /dvt1/src/com/example/anim_4 line 113 Java Problem
     这有很能是缺少某些重写函数,比如说在使用AnimationListener时,就有3种情况,虽然我们在应用程序中未必都使用了这3种情况,但是这3种情况的代码也得必须重写出来,否则会
     & 编译通过不了。
6)    如果在elipse中run as出现如下错误提示:emulator-5554 disconnected! Cancelling 'com.example.****.SplashActivity activity launch'!,则说明是仿真器没有连接上,解      决方法是:run as-&run configurations-&Android Application-&Target最后一个选项下面的仿真器前打钩即可。&
7)    android导入工程时出现如下错误:Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools & Fix Project Properties.
     解决方法是Project Properties -& java Compiler -& Enable project specific settings,然后继续选择 Compiler Compliance Leave(多尝试几个值) -& OK
8)   & &android中修改工程文件名的方法,需要同时更改src文件夹和gen文件夹,在elciplse工程中右击这2个文件夹,选择refactor--&rename即可。
9)    如果新加入的工程出现如下错误:R.java was removed! Recreating R.java! 说明是R.java文件没有生成成功,可以先把工程delete掉(不要从硬盘上delete掉),然后重新导入即可。
阅读(...) 评论()
阿萨德发斯蒂芬14:01 提问
关于提示eclipse上,“XX出错,程序已停止运行”的原因!
今天写了个小程序,在写按钮监听了时候出现了问题,反复检查代码,发现加上button.setOnClickListener(new Buttonlistener());这句话后,一运行,程序就终止运行,注释掉后,没问题,布局可以显示,本人菜鸟,望大神解答,如对问题有困惑,可指出,一直在,随时恭候。
按赞数排序
感觉把你的代码或者log信息粘出来或许更好,这样看不出来你的错误在哪
详细说下,先在布局里添加一个button和textview控件,然后在mainactivity里实现点击按钮设置textview的显示内容,个人感觉思路很简单啊,代码也没报错,就是一运行就终止,实在不知其所以然了。以下是代码,注意中间一行我给注释了,不注释就会运行停止。
package com.wh.test_
import android.app.A
import android.app.ActionB
import android.app.F
import android.os.B
import android.view.LayoutI
import android.view.M
import android.view.MenuI
import android.view.V
import android.view.View.OnClickL
import android.view.ViewG
import android.widget.B
import android.widget.TextV
import android.os.B
public class MainActivity extends Activity {
private TextView textV
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textview);
button.setOnClickListener(new Buttonlistener());
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
public class Buttonlistener implements OnClickListener{
public void onClick(View arg0) {
textView.setText("NIHAO ");
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return super.onOptionsItemSelected(item);
* A placeholder fragment containing a simple view.
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootV
82关注|594收录
135关注|244收录
630关注|595收录
其他相似问题
相关参考资料求助。Notification问题,程序一运行就崩溃,找不到原因。_android开发吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
求助。Notification问题,程序一运行就崩溃,找不到原因。收藏
package com.LiuPan.liupan_import android.support.v7.app.ActionBarAimport android.app.Nimport android.app.NotificationMimport android.content.Cimport android.graphics.BitmapFimport android.os.Bimport android.view.Mimport android.view.MenuIimport android.view.Vimport android.view.View.OnClickLimport android.widget.Bpublic class MainActivity extends ActionBarActivity {private Notifprivate NotificationMprivate B@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);notification = new Notification.Builder(this).setTicker(&接收到一个Notification&).setContentTitle(&Notification&).setContentText(&这是一条Notification&).setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND).setSmallIcon(android.R.drawable.ic_notification_clear_all).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)).setWhen(System.currentTimeMillis()).build();button = (Button) findViewById(R.id.button1);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubmanager.notify(1, notification);//发送一条通知}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// I this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {}return super.onOptionsItemSelected(item);}}
你直接在子线程做了事件,需要Handler通知下到UI展示
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或我用WindowManager.addView(view, params)方法添加了一个view,当我实现这个view的setOnTouchListener和setOnClickListener时,view只能监听ontouch事件,监听不到onclick事件,我去掉setOnTouchListener后就能监听到onclick事件,这是怎么回事?如何让这个view能监听到这2中事件?
该问题被发起重新开启投票
投票剩余时间:
之前被关闭原因:
该问题被发起删除投票
投票剩余时间:
距离悬赏到期还有:
参与关闭投票者:
关闭原因:
该问题已经被锁定
锁定原因:()
保护原因:避免来自新用户不合宜或无意义的致谢、跟帖答案。
该问题已成功删除,仅对您可见,其他人不能够查看。
OnTouch是指从接触到屏幕到离开屏幕的整个过程。onClick仅仅是指按下的那一霎那。
监听不到OnClick事件是因为在onTouch中这个事件被消费了,public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
你在这里返回为true.修改为false即可。
【0】这是因为回调函数的返回值所引起的。onClick是不需要返回值的,而onTouch的返回值是boolean.关于onTouch的返回值,官方解释如下:*This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.*如果在Touch事件的down action的时候返回false,则表示这个组件不会吃掉这个down event,并且对touch的后续动作都不感兴趣。那么整个touch的动作,从一开始刚点击下去的时候,你就不会再被回调到了。
官方还有一段注意的话:Note: Android will call event handlers first and then the appropriate default handlers from the class definition second. As such, returning true from these event listeners will stop the propagation of the event to other event listeners and will also block the callback to the default event handler in the View. So be certain that you want to terminate the event when you return true.如果某个Listener在回调到的时候返回了true,那么这个event就会被吃掉,而不能继续传播给其他的Listener。
【1】所以说如果在onTouch里面是返回true,那么onClick当然就无法再被回调了。
【2】可以用下面的代码测试下:
Button button = (Button)this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
// TODO Auto-generated method stub
Log.d("Test","This is OnClick");
button.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
// TODO Auto-generated method stub
Log.d("Test","This is onTouch,return false");
我已经验证过,只有在onTouch返回false的情况下,onClick才能被执行,这个时候他们两个算是共存的。当然我们还可以在onTouch里面去细分,可以根据MotionEvent的type去动态调整何时进行return true或者false。
补充知识:
【0】TouchMode : 对于触屏设备,当用户点击到屏幕的时候,设备会进入Touch Mode。如果某个组件的isFocusableInTouchMode()为true,那么当点击到这个组件的时候会触发组件进行focus。例如,EditText View,我们点击输入框的时候,会引发EditText进行focus,这个时候,可以看到光标在输入框进行跳动。那么其他组件如果没有设置isFocusableInTouchMode()为ture的话,就是touchable的,当点击的时候不会触发focus,只会触发onClick的Listener。
【1】onClick与onTouch的官方介绍:onClick()From View.OnClickListener. This is called when the user either touches the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses the suitable "enter" key or presses down on the trackball.可以看到这个onClick要么是在touchmode的情况下会被回调,要么是在非touchmode的情况下,当focus到的时候被回调。onTouch()From View.OnTouchListener. This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).那么onTouch就是类似press,release或者其他类似在屏幕上的移动。
onTouch返回True会屏蔽onClick事件。可以自定义一个Button,重写onTouchEvent,在其中处理Touch事件,最后返回true。这样应该可以让click事件和touch事件共存。
不是您所需,查看更多相关问题与答案
德问是一个专业的编程问答社区,请
后再提交答案
关注该问题的人
共被浏览 (24481) 次}

我要回帖

更多关于 android 重写view 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信