使用按钮在类之间切换 - Android Eclipse

时间:2012-12-12 00:56:06

标签: android eclipse class button

我想点击暂停按钮,然后在点击按钮时返回上一课程。我没有遇到Pause课,但后来我无法回来。一切看起来都是一样的,所以我认为这可能是一个普遍的等级问题或类似的东西,但我不确定。

调用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>

3 个答案:

答案 0 :(得分:2)

您对两个按钮使用相同的ID。更改其中一个ID。例如,将其命名为button2。与TextView相同,但它们不是引用方法。

答案 1 :(得分:1)

Caused by: java.lang.NoSuchMethodException: pauseCounter

您是否正在某处调用名为pauseCounter()的方法?你问题就在那里。

此外,您需要在引用(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();               
        }
    });
    }

  }
  

旁注:

尝试命名您的按钮ID。不要使用默认值,它可能导致很难跟踪的问题。 尝试将它们重命名为idBtnPause,idBtnResume,并确保不要复制名称

答案 2 :(得分:1)

您是否尝试过将onclick监听器设置为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);


        }
    });