当我按下按钮时,活动应该切换。我最初在XML中使用onClick来启动方法,但在阅读了很多关于它的答案后,我改为听众。但它仍然无法正常工作。 此外,当我在第一个活动中单击空白区域时,应用程序崩溃并出现相同的错误。
请帮忙!
MenuActivity:
package com.milkywhy.unidigid;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_layout);
Button btnNextScreen = (Button) findViewById(R.id.verify);
//Listening to button event
btnNextScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen = new Intent(getApplicationContext(), VerifyActivity.class);
startActivity(nextScreen);
}
});
Button btnNextScreen2 = (Button) findViewById(R.id.how);
//Listening to button event
btnNextScreen2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen2 = new Intent(getApplicationContext(), HowItWorksActivity.class);
startActivity(nextScreen2);
}
});
Button btnNextScreen3 = (Button) findViewById(R.id.getIt);
//Listening to button event
btnNextScreen3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen3 = new Intent(getApplicationContext(), IDActivity.class);
startActivity(nextScreen3);
}
});
}
@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, menu);
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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu_layout, container, false);
return rootView;
}
}
}
这是logCat:
09-14 17:25:16.034: D/dalvikvm(16194): Late-enabling CheckJNI
09-14 17:25:18.006: D/libEGL(16194): loaded /system/lib/egl/libEGL_mali.so
09-14 17:25:18.066: D/libEGL(16194): loaded /system/lib/egl/libGLESv1_CM_mali.so
09-14 17:25:18.076: D/libEGL(16194): loaded /system/lib/egl/libGLESv2_mali.so
09-14 17:25:18.117: D/OpenGLRenderer(16194): Enabling debug mode 0
09-14 17:25:18.227: D/dalvikvm(16194): GC_CONCURRENT freed 103K, 9% free 9619K/10567K, paused 16ms+4ms, total 99ms
09-14 17:55:23.522: D/AndroidRuntime(16194): Shutting down VM
09-14 17:55:23.522: W/dalvikvm(16194): threadid=1: thread exiting with uncaught exception (group=0x40fe62a0)
09-14 17:55:23.542: E/AndroidRuntime(16194): FATAL EXCEPTION: main
09-14 17:55:23.542: E/AndroidRuntime(16194): java.lang.IllegalStateException: Could not find a method verify(View) in the activity class com.milkywhy.unidigid.MenuActivity for onClick handler on view class android.widget.RelativeLayout
09-14 17:55:23.542: E/AndroidRuntime(16194): at android.view.View$1.onClick(View.java:3647)
09-14 17:55:23.542: E/AndroidRuntime(16194): at android.view.View.performClick(View.java:4162)
09-14 17:55:23.542: E/AndroidRuntime(16194): at android.view.View$PerformClick.run(View.java:17082)
09-14 17:55:23.542: E/AndroidRuntime(16194): at android.os.Handler.handleCallback(Handler.java:615)
09-14 17:55:23.542: E/AndroidRuntime(16194): at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 17:55:23.542: E/AndroidRuntime(16194): at android.os.Looper.loop(Looper.java:137)
09-14 17:55:23.542: E/AndroidRuntime(16194): at android.app.ActivityThread.main(ActivityThread.java:4867)
09-14 17:55:23.542: E/AndroidRuntime(16194): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 17:55:23.542: E/AndroidRuntime(16194): at java.lang.reflect.Method.invoke(Method.java:511)
09-14 17:55:23.542: E/AndroidRuntime(16194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
09-14 17:55:23.542: E/AndroidRuntime(16194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
09-14 17:55:23.542: E/AndroidRuntime(16194): at dalvik.system.NativeStart.main(Native Method)
09-14 17:55:23.542: E/AndroidRuntime(16194): Caused by: java.lang.NoSuchMethodException: verify [class android.view.View]
09-14 17:55:23.542: E/AndroidRuntime(16194): at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-14 17:55:23.542: E/AndroidRuntime(16194): at java.lang.Class.getMethod(Class.java:915)
09-14 17:55:23.542: E/AndroidRuntime(16194): at android.view.View$1.onClick(View.java:3640)
09-14 17:55:23.542: E/AndroidRuntime(16194): ... 11 more
答案 0 :(得分:0)
在致电getApplicationContext()
时,您是否收到了找不到方法的错误?
如果是这样,您可以试试MenuActivity.this.getApplicationContext()
是否正常工作?
我一直都是非常安全的编程,因此很少使用内部类(更不用说匿名类了)。因此,通常我使用相同的外部类(在本例中为MenuActivity)来处理点击。因此,我会说this.getApplicationContext()
。
现在我们在一个内部阶级中,事情可能会变得有些模糊。