无法使用按钮执行操作

时间:2012-03-08 04:29:19

标签: android

我的活动是我的应用程序主要活动之后的第二个活动,它有按钮,当我点击时,我无法执行任何操作。我猜这个问题是跟随两个文件。请告诉我

package org.bhoomi.quotes;
public class ApageActivity extends Activity implements OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.apage);

    View aeButton=findViewById(R.id.ae_button);
    aeButton.setOnClickListener(this);
    View mgButton=findViewById(R.id.mg_button);
    mgButton.setOnClickListener(this);    
}
public void onClick(View v){
// TODO Auto-generated method stub
switch (v.getId())
 {
    case R.id.ae_button:
                Intent i=new Intent(this,Apage.class);
                startActivity(i);
                break;
        case R.id.mg_button:    finish();
                    break;
 }
}
}

我已经有了一个文件QuotesActivity.java,它的代码几乎相同但工作正常但不是这个。 和其他是 Quote Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.bhoomi.quotes"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".QuotesActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Apage"
        android:label="@string/menulabel"></activity>
    <activity
        android:name=".ApageActivity">
         <intent-filter>
            <action android:name="android.intent.action.APAGE" />
        </intent-filter>

    </activity>
</application>

</manifest>

我在

下添加了我的logcat
D/AndroidRuntime(716): Shutting down VM
W/dalvikvm(716): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(716): FATAL EXCEPTION: main
E/AndroidRuntime(716): java.lang.IllegalStateException: Could not find a method   onClickHandler(View) in the activity class org.bhoomi.quotes.Apage for onClick handler on  view class android.widget.Button with id 'mg_button'
E/AndroidRuntime(716):  at android.view.View$1.onClick(View.java:2059)
E/AndroidRuntime(716):  at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(716):  at android.view.View$PerformClick.run(View.java:8816)
E/AndroidRuntime(716):  at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(716):  at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(716):  at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(716):  at  android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(716):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(716):  at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(716):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(716):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(716):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(716): Caused by: java.lang.NoSuchMethodException: onClickHandler
E/AndroidRuntime(716):  at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
E/AndroidRuntime(716):  at java.lang.Class.getMethod(Class.java:985)
E/AndroidRuntime(716):  at android.view.View$1.onClick(View.java:2052)
E/AndroidRuntime(716):  ... 11 more

3 个答案:

答案 0 :(得分:1)

public void onClick(View v)
{
  Intent localIntent;
  switch (v.getId())
  {
  case R.id.ae_button:

    localIntent = new Intent(ApageActivity.this, Apage.class);
    ApageActivity.this.startActivity(localIntent);

 case R.id.mg_button:    

 finish();
 break;

  }
}

也不确定这是否重要,而不是

View aeButton=findViewById(R.id.ae_button);
aeButton.setOnClickListener(this);
View mgButton=findViewById(R.id.mg_button);
mgButton.setOnClickListener(this);   

尝试

Button aeButton=(Button) findViewById(R.id.ae_button);
aeButton.setOnClickListener(this);
Button mgButton=(Button) findViewById(R.id.mg_button);
mgButton.setOnClickListener(this);   

答案 1 :(得分:0)

Button aeButton=(Button) findViewById(R.id.ae_button);
aeButton.setOnClickListener(this);
Button mgButton=(Button) findViewById(R.id.mg_button);
mgButton.setOnClickListener(this);  

 setOnClickListener(new OnClickListener() {

public void onClick(View v) {       

  switch (v.getId())
   {
      case R.id.ae_button:
             Intent i=new Intent(this,Apage.class);
             startActivity(i);
             break;
    case R.id.mg_button:   
             finish();
              break;
  }
 }
});

试试这个Quote Manifest.xml

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="org.bhoomi.quotes"
 android:versionCode="1"
 android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
    android:name=".QuotesActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".Apage"   />     
 <activity android:name=".ApageActivity" />

  </intent-filter>
 </activity>
 </application>

</manifest>

答案 2 :(得分:0)

我确定你的程序中确实存在问题,但是让我建议另一种方法来为你的按钮实现onClick动作。

第1步: 只需使用android:onClick属性在XML布局中定义Button,并将值作为方法名称。

例如:

<Button
  ....
   ....
  android:onClick="onClickHandler"/>

现在定义一个具有相同名称的公共方法:

public void onClickHandler(View v){

    /**do your stuffs whenever click action happen**/

}