简体   繁体   English

Onclick方法在Android Button中不起作用

[英]Onclick method is not working in android Button

I was studying of Onclick events and the listeners in android. 我正在研究Onclick事件和android中的监听器。 On the way, I created a sample app and my aim is to save the given number (register.java) in the database and to show it in an another activity (main.java). 在路上,我创建了一个示例应用程序,我的目标是将给定的数字(register.java)保存在数据库中,并在另一个活动(main.java)中显示它。 But, now on clicking the 'save' button, nothing has been happening. 但是,现在点击“保存”按钮,一切都没有发生。 Even the toast method is also not working. 即使是吐司方法也行不通。

This is my code: 这是我的代码:

 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    data = register.this.openOrCreateDatabase("Number", MODE_PRIVATE, null);
    data.execSQL("CREATE TABLE IF NOT EXISTS table1(number varchar(15));");

    e1 = (EditText)findViewById(R.id.mob_num);
    b1 = (Button)findViewById(R.id.save);
    b2 = (Button)findViewById(R.id.go);

b1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        number = e1.getText().toString();
        data.execSQL("INSERT INTO table1 VALUES('"+number+"')");
        Toast.makeText(getApplicationContext(), "'"+number+"'successfully inserted",Toast.LENGTH_SHORT).show();
        Intent i = new Intent(register.this, main.class);
        startActivity(i);
        finish();

    }
});

b2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i =new Intent(register.this,main.class);
        startActivity(i);
        data.close();
        finish();
    }
});

}

Here is my manifest.xml: 这是我的manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.a.a"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity android:name=".main"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>     
        </intent-filter>


    </activity>
</application>

</manifest>

I know this a very basic thing in android. 我知道这是android中的一个非常基本的东西。 But, I hope you may help me in this. 但是,我希望你能帮助我。 Sorry and Thanks for your time. 对不起,谢谢你的时间。

There's an easy way to register onClickListeners in Android: In your declaration of the button add android:onClick="onClick" and create a method in the Activity containing the button called onClick(View v) . 在Android中注册onClickListeners有一种简单的方法:在你的按钮声明中添加android:onClick="onClick"并在Activity中创建一个包含名为onClick(View v)的按钮的方法。 From here you can just 从这里你可以

switch(v) {
    case R.id.save:
         Toast.makeText(this, "save button has been pressed", Toast.LENGTH_LONG).show();
         break;
    case R.id.go:
         Toast.makeText(this, "go button has been pressed", Toast.LENGTH_LONG).show();
         break;
         }

and so on until you've registered all the buttons in the activity. 等等,直到您注册了活动中的所有按钮。 Of course you can use other variable names than what I did in this example, just remember the onClick method (or whatever you name it) MUST be called with View as parameter and the switch MUST run on that parameter. 当然你可以使用其他变量名,而不是我在这个例子中所做的,只记得onClick方法(或任何你命名的方法)必须用View作为参数调用,并且开关必须在该参数上运行。

Just try this simple approach to check button click and tell me if it works: 只需尝试这种简单的方法来检查按钮点击并告诉我它是否有效:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
 import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
private Button closeButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    this.closeButton = (Button)this.findViewById(R.id.button);
    this.closeButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
     Toast.makeText(this, "You clicked the button", Toast.LENGTH_SHORT).show();

}
}
Button btn,
btnback = (Button) findViewById(R.id.activity_essentials_btnback);

btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                                Toast.makeText(v.getContext(), "Click here",
                        Toast.LENGTH_SHORT).show();

            }
        });

The Logcat provided does not give any information, Provide the correct logcat. 提供的Logcat不提供任何信息,提供正确的logcat。

The code for the click listeners is right. 点击侦听器的代码是正确的。 The probable exception is only the null pointer exception. 可能的异常只是空指针异常。 So provide null checks to all the data base creation and insertion. 因此,对所有数据库的创建和插入提供空值检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM