无法获取OnClick以触发按钮

时间:2013-01-20 07:24:04

标签: android triggers onclick onclicklistener

我知道还有其他类似的帖子,但他们都没有一个对我有用的解决方案。

代码:http://pastebin.com/LSC3eH8p 回复:https://github.com/josiah14/FuelConsumptionTutorial

单击按钮时,ConsCalc活动中的“添加记录”按钮的OnClick事件未触发。起初我认为这是一个热插拔问题,但我已经在我的最后两次测试之间完全卸载了应用程序,并且回调仍然没有被触发。这是怎么回事?

public class AddRecord extends Activity {

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

        // Show the Up button in the action bar.
//      getActionBar().setDisplayHomeAsUpEnabled(true);
    }

public class CalcMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calc_menu);

    // create a button object
    final Button buttonAddRecord = (Button) findViewById(R.id.buttonAddRecord);
    // attach a listener to the button
    buttonAddRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // create an Intent object for this activity, including its destination class
            Intent myIntent = new Intent(CalcMenu.this, AddRecord.class);
            // run the activity
            startActivity(myIntent);
        }
    });

和清单......

        <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="idig.za.net.conscalc"
        android:versionCode="1"
        android:versionName="1.0" >

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

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="idig.za.net.conscalc.GetRegistration"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name="idig.za.net.conscalc.SplashScreen" >
            </activity>
            <activity android:name="idig.za.net.conscalc.CalcMenu" >
            </activity>
            <activity android:name="idig.za.net.conscalc.SearchRecord" >
            </activity>
            <activity android:name="idig.za.net.conscalc.AddRecord">
            </activity>
        </application>

    </manifest>

1 个答案:

答案 0 :(得分:1)

查看您的pastebin,似乎您使用空的OnClickListeners多次为buttonAddRecord设置onClickListener。我想你可能有一个复制粘贴错误,你打算为不同的按钮设置onClickListener。

更正代码:

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

    // create a button object
    final Button buttonAddRecord = (Button) findViewById(R.id.buttonAddRecord);
    // attach a listener to the button
    buttonAddRecord.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            // create an Intent object for this activity, including its destination class
                            Intent myIntent = new Intent(CalcMenu.this, AddRecord.class);
                            // run the activity
                            startActivity(myIntent);
                    }
            });

    // create a button object
    final Button buttonCalculateConsumption = (Button) findViewById(R.id.buttonCalculateConsumption);
    // attach a listener to the button
    buttonCalculateConsumption.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                              // create an Intent object for this activity, including its destination class
//                              Intent myIntent = new Intent(CalcMenu.this, CalculateConsumption.class);
//                              // run the activity
//                              startActivity(myIntent);
                    }
            });

    // create a button object
    final Button buttonDeleteRecord = (Button) findViewById(R.id.buttonDeleteRecord);
    // attach a listener to the button
    buttonDeleteRecord.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                              // create an Intent object for this activity, including its destination class
//                              Intent myIntent = new Intent(CalcMenu.this, DeleteRecord.class);
//                              // run the activity
//                              startActivity(myIntent);
                    }
            });

    // create a button object
    final Button buttonEditRecord = (Button) findViewById(R.id.buttonEditRecord);
    // attach a listener to the button
    buttonEditRecord.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            // TODO Auto-generated method stub
                    }
            });
}