0

我知道还有其他类似的帖子,但没有一个有适合我的解决方案。

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

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

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>
4

1 回答 1

1

查看您的 pastebin,您似乎为 buttonAddRecord 设置了几次 onClickListener,而 OnClickListeners 为空。我认为您可能有一个复制粘贴错误,您打算为不同的按钮设置 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
                    }
            });
}
于 2013-01-20T07:43:24.110 回答