【发布时间】:2015-09-03 18:17:10
【问题描述】:
我在 2 个程序中使用了 Countdowntimer。一个在 MainActivity 中工作得很好,另一个在子类中不起作用。我应该如何纠正这个问题。
工作正常:
public class MainActivity extends Activity implements View.OnClickListener {
TextView text;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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_main, menu);
button = (Button)findViewById(R.id.startbutton);
text = (TextView)findViewById(R.id.text);
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();
}
}
但是这个没有用,即使它是相同的代码但在一个子类中
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)
【问题讨论】:
-
那么什么不起作用?它会崩溃吗?有没有可以添加的相关日志?
-
是的,它崩溃了,我已经用第二组代码附加了日志
-
请输入第二类的代码,以便清楚您是如何使用子类的,因为在上面的代码中我没有看到任何子类
-
我认为您需要了解如何设置按钮的 setonclick 侦听器。参考这个stackoverflow.com/questions/5588804/…
标签: android