我想点击暂停按钮,然后在点击按钮时返回上一课。我去 Pause 课没有问题,但是我不能回来。一切看起来都一样,所以我认为这可能是一个一般的层次结构问题或类似的问题,但我不确定。
暂停类被调用并正确运行,但是当我按下按钮时,程序崩溃了。我正试图回到我刚离开的同一个班级。我正在使用 Eclipse。谢谢你的帮助!这是我的非工作代码:
package com.evorlor.counter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
public class Pause extends Activity {
@Override
protected void onCreate(Bundle instPause) {
super.onCreate(instPause);
setContentView(R.layout.activity_counter);
findViewById(R.id.textView1);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(150);
tv.setText("PAUSE");
}
public void resumeCounter(View view) {
Intent resume = new Intent(Pause.this, Counter.class);
startActivity(resume);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Pause" >
<TextView
android:id="@+id/tvPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/btnResume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="resumeCounter" />
</RelativeLayout>
12-12 01:02:27.796: E/AndroidRuntime(1883): FATAL EXCEPTION: main
12-12 01:02:27.796: E/AndroidRuntime(1883): java.lang.IllegalStateException: Could not find a method pauseCounter(View) in the activity class com.evorlor.counter.Pause for onClick handler on view class android.widget.Button with id 'button1'
12-12 01:02:27.796: E/AndroidRuntime(1883): at android.view.View$1.onClick(View.java:3584)
12-12 01:02:27.796: E/AndroidRuntime(1883): at android.view.View.performClick(View.java:4202)
12-12 01:02:27.796: E/AndroidRuntime(1883): at android.view.View$PerformClick.run(View.java:17340)
12-12 01:02:27.796: E/AndroidRuntime(1883): at android.os.Handler.handleCallback(Handler.java:725)
12-12 01:02:27.796: E/AndroidRuntime(1883): at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 01:02:27.796: E/AndroidRuntime(1883): at android.os.Looper.loop(Looper.java:137)
12-12 01:02:27.796: E/AndroidRuntime(1883): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-12 01:02:27.796: E/AndroidRuntime(1883): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 01:02:27.796: E/AndroidRuntime(1883): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 01:02:27.796: E/AndroidRuntime(1883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-12 01:02:27.796: E/AndroidRuntime(1883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-12 01:02:27.796: E/AndroidRuntime(1883): at dalvik.system.NativeStart.main(Native Method)
12-12 01:02:27.796: E/AndroidRuntime(1883): Caused by: java.lang.NoSuchMethodException: pauseCounter [class android.view.View]
12-12 01:02:27.796: E/AndroidRuntime(1883): at java.lang.Class.getConstructorOrMethod(Class.java:460)
12-12 01:02:27.796: E/AndroidRuntime(1883): at java.lang.Class.getMethod(Class.java:915)
12-12 01:02:27.796: E/AndroidRuntime(1883): at android.view.View$1.onClick(View.java:3577)
12-12 01:02:27.796: E/AndroidRuntime(1883): ... 11 more
计数器类
package com.evorlor.counter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class Counter extends Activity {
private int count = 0;
private int hiCount = 0;
private boolean capCount = false;
@Override
protected void onCreate(Bundle instCounter) {
super.onCreate(instCounter);
setContentView(R.layout.activity_counter);
findViewById(R.id.textView1);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setTextSize(250);
if (count < 10000 && capCount == false) {
tv.setText(Integer.toString(count));
} else {
capCount = true;
if (count >= 10000) {
hiCount += 10;
count -= 10000;
}
if (hiCount < 100) {
tv.setText(hiCount + "k+" + count);
} else {
tv.setText("Over\n100k");
}
}
tv.setGravity(Gravity.CENTER);
}
public void pauseCounter(View view) {
Intent pause = new Intent(Counter.this, Pause.class);
startActivity(pause);
}
}
柜台布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Counter" >
<TextView
android:id="@+id/tvCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="@+id/btnPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="pauseCounter" />
</RelativeLayout>