Reputation: 7553
I want to hit the pause button, then return to the previous class when buttons are clicked. I am having no trouble going to the Pause class, but then I cannot get back. Everything seems identical, so I think this may be a general hierarchy question or something like it, but I am not sure.
Pause class is called and runs correctly, but when I press the button, the program crashes. I am trying to return to the same class which I just left. I am using Eclipse. Thanks for your help! Here is my non-working code:
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
Counter Class
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);
}
}
Counter layout
<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>
Upvotes: 0
Views: 507
Reputation: 22527
Caused by: java.lang.NoSuchMethodException: pauseCounter
Are you refering somewhere to a method called pauseCounter()? You problem lays there.
Furthermore,y ou need to set the layout first before reference(findViewById)
public class Pause extends Activity {
@Override
protected void onCreate(Bundle instPause) {
super.onCreate(instPause);
setContentView(R.layout.activity_counter);
findViewById(R.id.textView1); // move this here
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(150);
tv.setText("PAUSE");
Button resume = (Button) findViewById(R.id.button1);
resume.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
Side note:
Try to name your button ids. Dont use default, it can cause problems very difficult to track. try renaming them like this idBtnPause, idBtnResume and make sure you dont duplicate the names
Upvotes: 1
Reputation: 500
have you tried setting an onclick listener to tv
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent resume = new Intent(Pause.this, Counter.class);
startActivity(resume);
}
});
Upvotes: 1
Reputation: 36449
You are using the same id for both buttons. Change one of the ids. Name it button2
for example. Same with your TextViews, but they aren't referencing methods.
Upvotes: 2