的onClick监听器不工作越来越没有错误 [英] onClick Listener not working getting no errors

查看:246
本文介绍了的onClick监听器不工作越来越没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有得到的编码问题进行的onClick正常工​​作我有一个包含8个按键键1应打开脸书的页面2,4,5,6和7个主要活动应该打开一个网页浏览器来定义的页面和8键打开电话拨号器preset的号码打电话。我不能让这跟出去打工崩溃的应用程序,我可以在一个的onclick监听器设置为1键得到它的工作,但是当我添加下一个崩溃的应用程序,我得到日食没有错误我被告知做它处理onclicks但片段即时通讯失去了如何做到这一点任何一个可以帮我这个编码???

这是我迄今只为2个按钮仍然没有得到一打开脸书或一打开电话拨号器,但是这是据我得到那么它崩溃 - 杰里33分钟前

 进口android.net.Uri;
进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Intent;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;公共类MainActivity延伸活动{    @覆盖保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        addButtonClickListner();
    }    公共无效addButtonClickListner(){
        按钮btnNavigator =(按钮)findViewById(R.id.imageButton2);
        btnNavigator.setOnClickListener(新OnClickListener(){
             公共无效的onClick(查看ARG){
                  意向意图=新意图(Intent.ACTION_VIEW,Uri.parse()WWW *****融为一体。);
                  startActivity(意向);
             }
        });
    }


解决方案

做到这一点,而不是

...

 包com.example.test code;进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
//测试code:进口android.widget.TextView;//让你的类直接实现OnClickListener接口。这个
//将让你使用onClickListener
类MainActivity扩展活动实现OnClickListener {
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //查找我们通过自己的ID在我们的活动的布局视图
        按钮按钮1 =(按钮)findViewById(R.id.button1);
        按钮按钮2 =(按钮)findViewById(R.id.button2);
        // ......继续按钮3 - 按钮8        //设置我们的点击听众我们每个按钮
        button1.setOnClickListener(本);
        button2.setOnClickListener(本);
        // ......继续按钮3 - 8按钮
    }    //因为我们的类实现OnClickListener接口
    //这将是监听点击。正因为如此,我们可以
    //覆盖点击监听器的默认的onClick(视图V)方法。
    //视图V是我们的观点,还是我们的按钮,那就是点击。
    @覆盖
    公共无效的onClick(视图v){
        //测试code:TextView的文本=(TextView的)findViewById(R.id.text);        //这是一个可以让您的每一个按钮的声明
        //执行不同的过程。对于我的测试code,我有每个
        //键复位我已经显示在顶部的TextView的我
        //布局。
        开关(v.getId()){
        案例R.id.button1:
            //测试code:text.setText(按钮1);
            打破;
        案例R.id.button2:
            //测试code:text.setText(按钮2);
            打破;
        // ......继续按钮3 - 8按钮
        默认:
            Log.d(getApplication()getPackageName()按钮点击错误!);
            打破;
        }
    }
}

请看看我的意见。被评价为

的部分

  //测试code:

为code是pretty为你在做什么无用。从switch语句测试code是您需要什么,你想与每个按钮做什么来替代。同样重要的是要注意,在这里我说的// ......继续按钮3 - 按钮8。我只是意味着重复,我已经开始与前两个按钮,其余六个模式

如果你想和我定测试code测试它,这是我的布局(只需确保您清除//测试code:,以便测试code线路都没有再注释掉:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    工具:上下文=MainActivity。>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=preSS按钮
        机器人:ID =@ + ID /文本/>    <按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=按钮1
        机器人:ID =@ + ID /按钮1/>    <按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=按钮2
        机器人:ID =@ + ID /按钮2/>< / LinearLayout中>

注:

要明白,在好的xml的布局中,是很重要的机器人:文本=部分将被设置为'机器人:文本= @字符串/string_id',而不是一些普通的字符串,以帮助定位。

请确保您更改声明封装到任​​何包你的活动放在最顶端一行。这个包的名称以应用程序/ src目录/程序包被发现,通常是类似com。示例.applicationname

我也想说,我已经包括在code中的意见,以便它可以被复制和粘贴,而无需一遍又一遍,以防有人想使用code作为重新审视这个链接指导,不是因为我想张贴在回答吨code的。

i am having issues getting the coding for onClick to work correctly i have a main activity that contains 8 buttons button 1 should open face book to a page 2,4,5,6 and 7 should open a web browser to defined page and button 8 to open phone dialer with preset number to call. I can not get this to work with out crashing the app i can set one onclick listener to button 1 and get it to work but when i add the next one it crashes the app i get no errors in eclipse i was told to do it with a fragment to handle the onclicks but im lost on how to do this can any one help me with this coding???

This is what i have so far just for 2 buttons still haven't got the one to open face book or the one to open phone dialer but this is as far as i get then it crashes – Jerry 33 mins ago

import android.net.Uri;
import android.os.Bundle; 
import android.app.Activity;
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

    @Override protected void onCreate(Bundle savedInstanceState) {           
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        addButtonClickListner(); 
    } 

    public void addButtonClickListner() { 
        Button btnNavigator = (Button)findViewById(R.id.imageButton2);                
        btnNavigator.setOnClickListener(new OnClickListener(){ 
             public void onClick(View arg) { 
                  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("www.*****.com")); 
                  startActivity(intent); 
             } 
        }); 
    } 

解决方案

Do this instead

....

package com.example.testcode;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
// Test code: import android.widget.TextView;

// Let your class implement the OnClickListener interface directly. This
// will let you use the onClickListener
class MainActivity extends Activity implements OnClickListener{
    @Override 
    public void onCreate(Bundle savedInstanceState) {           
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 

        // "find" our views by their id's in our activity's layout
        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);
        //...... continue for button3 - button8

        // set our "click" listeners for each of our buttons
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        //...... continue for button3 - button 8
    } 

    // Because our class implements the OnClickListener interface
    // it will be listening for "clicks". Because of this, we can
    // override the click listener's default onClick(View v) method.
    // View v is our view, or our button, that is "clicked".
    @Override
    public void onClick(View v) {
        // Test code: TextView text = (TextView) findViewById(R.id.text);

        // This is the statement that will allow each of your buttons
        // to perform different processes. For my test code, I have each
        // button reset the TextView I have displayed in the top of my
        // layout.
        switch(v.getId()){  
        case R.id.button1:
            // Test code: text.setText("Button 1");
            break;
        case R.id.button2:
            // Test code: text.setText("Button 2");
            break;
        //...... continue for button3 - button 8
        default:
            Log.d(getApplication().getPackageName(), "Button click error!");
            break;
        }   
    }
} 

Please take a look at my comments. The portion that is commented as

// Test Code: 

is code that is pretty much useless for what you are doing. The Test Code from the switch statement is what you need to replace with what you want to do with each individual button. It is also important to note that where I say "//...... continue for button3 - button8" I simply mean to repeat the pattern that I have started with the first two buttons with the remaining six.

If you would like to test it with my given test code, here is my layout (just make sure you erase "// Test Code: " so that the test code lines are no longer commented out:

<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:text="Press a button"
        android:id="@+id/text" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:id="@+id/button1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:id="@+id/button2" />

</LinearLayout>

NOTES:

It is important to understand that in "good" xml layouts, the "android:text=" portion will be set to 'android:text=@string/"string_id"' instead of some general string to help with localization.

Make sure you change the very top line that declares the package to whatever package you have the Activity placed in. The name of this package is found at "Application"/src/"package_name" and is usually something like com.example.applicationname

I also want to say that I have included the comments in the code so that it can be copied and pasted without having to revisit this link over and over again in case anyone wants to use the code as a guide, not because I wanted tons of code posted in the answer.

这篇关于的onClick监听器不工作越来越没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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