倒计时器不在子类中工作

时间:2015-09-03 18:17:10

标签: android

我在2个程序中使用了Countdowntimer。一个在MainActivity中完美地工作,而另一个在子类中不起作用。我该怎么纠正这个。

工作正常:

public class AddLevel1 extends Activity implements View.OnClickListener {
    TextView text;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_level1);
        Intent in = getIntent();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_add_level1, menu);
        TextView text = (TextView) findViewById(R.id.timerText);
       Button button = (Button)findViewById(R.id.startbutton);
        button.setOnClickListener(this);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                text.setText(" " + String.format("%d min,%d sec", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
            }

            @Override
            public void onFinish() {
            text.setText("Done !!!");

            }
        }.start();
    }

}

Here is the log :

    --------- beginning of crash
09-03 17:55:40.830    2369-2369/com.example.ranjiniharihara.trial E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.ranjiniharihara.trial, PID: 2369
    java.lang.IllegalStateException: Could not find a method onButtonClick(View) in the activity class com.example.ranjiniharihara.trial.AddLevel1 for onClick handler on view class android.widget.Button with id 'startbutton'
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NoSuchMethodException: onButtonClick [class android.view.View]
            at java.lang.Class.getMethod(Class.java:664)
            at java.lang.Class.getMethod(Class.java:643)
            at android.view.View$1.onClick(View.java:4000)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

但即使它是一个相同的代码但是在一个子类中,这个确实不起作用

$(".f-remove-button").click(function(){
            console.log("remove-clicked");
             var userIds = [];
            $.each($("input[name='userIds']:checked"),
            function(){
                userIds.push($(this).val());
            });
            console.log(userIds);
            var url = "/clubtivity/user/remove-contacts"
            $.ajax({
                type:'POST',
                url:url,
                dataType:"json",
                data:{"userIds":userIds},
                success:function(response){

                }
            });

        });

1 个答案:

答案 0 :(得分:0)

关键是错误的这一部分:

Caused by: java.lang.NoSuchMethodException: onButtonClick [class android.view.View]

这意味着您的xml具有android:onClick="onButtonClick"属性。删除该属性或将该方法添加到您的Activity将解决崩溃问题。