0

I am writing an android app and am trying to start an activity from a button

The button calls the function startDb(), which is supposed to start the DB_manager activity.

The code and LogCat are shown below:

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();
    }
}

It gives the following error:

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
4

2 回答 2

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();
}

我假设你定义了你的OnClickinxml。您需要View在函数头中作为参数。

此外,看起来您可能在 xml 中定义它,例如

android:onClick="startDb()"

它应该是

android:onClick="startDb"

不 ”()”

从文档

为了使其工作,该方法必须是公共的并接受一个视图作为它的唯一参数。例如:

于 2013-07-03T16:21:03.033 回答
1

如果在 xml 中为按钮定义 onClick 属性

   public void startDb(View v) {
    Intent activity1 = new Intent(MainActivity.this, DB_manager.class);
    startActivity(activity1);
    //finish();
     }

你也可以删除这个

    public void Main(){

    }
于 2013-07-03T16:21:45.730 回答