我正在编写一个Android应用程序,并尝试从按钮开始一个活动
该按钮调用函数startDb(),该函数应该启动DB_manager活动。
代码和LogCat如下所示:
public class MainActivity extends Activity {
public void Main(){
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void startDb() {
Intent activity1 = new Intent(getApplicationContext(), DB_manager.class);
startActivity(activity1);
//finish();
}
}
它出现以下错误:
07-03 13:11:26.525: E/AndroidRuntime(19273): FATAL EXCEPTION: main
07-03 13:11:26.525: E/AndroidRuntime(19273): java.lang.IllegalStateException: Could not
find a method startDb()(View) in the activity class com.example.quiz.MainActivity for
onClick handler on view class android.widget.Button with id 'edit_db'
07-03 13:11:26.525: E/AndroidRuntime(19273): at android.view.View$1.onClick(View.java:3647)
07-03 13:11:26.525: E/AndroidRuntime(19273): at android.view.View.performClick(View.java:4162)
07-03 13:11:26.525: E/AndroidRuntime(19273): at android.view.View$PerformClick.run(View.java:17082)
07-03 13:11:26.525: E/AndroidRuntime(19273): at android.os.Handler.handleCallback(Handler.java:615)
07-03 13:11:26.525: E/AndroidRuntime(19273): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 13:11:26.525: E/AndroidRuntime(19273): at android.os.Looper.loop(Looper.java:137)
07-03 13:11:26.525: E/AndroidRuntime(19273): at android.app.ActivityThread.main(ActivityThread.java:4856)
07-03 13:11:26.525: E/AndroidRuntime(19273): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 13:11:26.525: E/AndroidRuntime(19273): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 13:11:26.525: E/AndroidRuntime(19273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-03 13:11:26.525: E/AndroidRuntime(19273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-03 13:11:26.525: E/AndroidRuntime(19273): at dalvik.system.NativeStart.main(Native Method)
07-03 13:11:26.525: E/AndroidRuntime(19273): Caused by: java.lang.NoSuchMethodException: startDb() [class android.view.View]
07-03 13:11:26.525: E/AndroidRuntime(19273): at java.lang.Class.getConstructorOrMethod(Class.java:460)
07-03 13:11:26.525: E/AndroidRuntime(19273): at java.lang.Class.getMethod(Class.java:915)
07-03 13:11:26.525: E/AndroidRuntime(19273): at android.view.View$1.onClick(View.java:3640)
07-03 13:11:26.525: E/AndroidRuntime(19273): ... 11 more
答案 0 :(得分:1)
更改
public void startDb() {
Intent activity1 = new Intent(getApplicationContext(), DB_manager.class);
startActivity(activity1);
//finish();
}
到
public void startDb(View v) {
Intent activity1 = new Intent(getApplicationContext(), DB_manager.class);
startActivity(activity1);
//finish();
}
我假设您定义了OnClick
inxml。您需要在函数标题中将View
作为参数。
此外,您可能会在xml中定义它,如
android:onClick="startDb()"
应该是
android:onClick="startDb"
否"()"
为了使其正常工作,该方法必须是公共的,并接受View作为其唯一参数。例如:
答案 1 :(得分:1)
如果在xml中为按钮定义onClick属性
public void startDb(View v) {
Intent activity1 = new Intent(MainActivity.this, DB_manager.class);
startActivity(activity1);
//finish();
}
您也可以删除此
public void Main(){
}