设置onclicklistener时应用程序崩溃 [英] Application crashes when i set an onclicklistener

查看:85
本文介绍了设置onclicklistener时应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码 帮帮我.如果我删除select.setonclicklistener.它的工作原理就像一个咒语,但是添加此行会使应用程序崩溃.甚至不显示此类.

Here is my code Help me out guys. if I remove the select.setonclicklistener. It works like a charm but adding this line crashes the application. Doesn't even show this class.

public class Play extends Activity implements OnClickListener{
private Button sideshow;
private int i = 0; 
private ImageView[] user_images = new ImageView[6];
private int noc;
private int[] user_cards_ids = {R.id.card_1,R.id.card_2,R.id.card_3,R.id.card_4,R.id.card_5,R.id.card_6};
private ArrayList<Integer> user_cards;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.play);
    //Define
    sideshow = (Button) findViewById(R.id.sideshow);
    Bundle extras = getIntent().getExtras();
    noc = extras.getInt("noc");
    //End
    sideshow.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(Play.this, "Sideshow!", Toast.LENGTH_LONG).show();
        }
    }); 
}
@Override
public void onClick(View v) {
    if(v.getId()==sideshow.getId())
        Toast.makeText(Play.this, "Sideshow", Toast.LENGTH_LONG).show();
}

LOGCAT

11-01 19:55:35.000: E/AndroidRuntime(16587): in writeCrashedAppName, pkgName     :com.example.flash_teenpatti
11-01 19:55:35.000: E/AndroidRuntime(16587): FATAL EXCEPTION: main
11-01 19:55:35.000: E/AndroidRuntime(16587): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flash_teenpatti/com.example.flash_teenpatti.Play}: java.lang.NullPointerException
11-01 19:55:35.000: E/AndroidRuntime(16587):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at android.os.Looper.loop(Looper.java:130)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at android.app.ActivityThread.main(ActivityThread.java:3683)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at java.lang.reflect.Method.invokeNative(Native Method)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at java.lang.reflect.Method.invoke(Method.java:507)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at dalvik.system.NativeStart.main(Native Method)
11-01 19:55:35.000: E/AndroidRuntime(16587): Caused by: java.lang.NullPointerException
11-01 19:55:35.000: E/AndroidRuntime(16587):    at com.example.flash_teenpatti.Play.onCreate(Play.java:34)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-01 19:55:35.000: E/AndroidRuntime(16587):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-01 19:55:35.000: E/AndroidRuntime(16587):    ... 11 more
11-01 19:55:35.050: E/ActivityManager(14462): Exception in bstSendTopActivityInfo while sending HttpPost: Connection to h t t p://10 . 0 . 2 . 2:2861 refused
11-01 19:55:35.980: E/ActivityManager(14462): Exception in bstSendTopActivityInfo while sending HttpPost: Connection to h t t p://10 . 0 . 2 . 2 :2861 refused
11-01 19:55:36.010: E/BstCommandProcessor-Application(14907): Exception in sendHttpRequest: Connection to h t t p://10.0.2.2:2861 refused

推荐答案

您已经在类定义中实现了OnClickListener并进行了内联定义.我建议您使用已实现的类.

You have implemented OnClickListener in the class definition and defined in-line. I would suggest you use the implemented class.

public class Play extends Activity implements OnClickListener{

因此,您只需将onclicklistener设置为 this 并取出嵌入式处理程序即可.

So you simply set the onclicklistener to this and take out the in-line handler.

    sideshow.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if(v.getId()==sideshow.getId())
    Toast.makeText(Play.this, "Sideshow", Toast.LENGTH_LONG).show();
}

实现的好处是其他类可以看到实现了什么并实际使用了这些功能.例如,在片段类中,您可以轻松地在活动类中调用方法(如果已实现).

The benefit of implements is other classes can see what is implemented and actually use the functions. For example in a fragment class you easily call methods in the activity class if they are implemented.

    Activity activity = this.getActivity();
    if (OnClickListener.class.isInstance(activity)) {
    OnClickListener adl = (OnClickListener) activity;
            adl.onClick(myView);
    }

这篇关于设置onclicklistener时应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆