android - onclick 找不到方法

android - onclick could not find method

我遇到了一个问题,即 onclick 找不到它应该执行的方法。 Android 尝试调用 onClick 方法 public void fahrplan(View v,Connection conn) 但无法找到它。我的代码的详细信息如下,之后是错误。请帮助我确定为什么 android 无法找到 onClick 方法。谢谢。

现在只有一个 activity,所以没有其他代码 运行。

MainActivity.java

    package eu.rathenau.fahrgemeinschaft;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    public class MainActivity extends AppCompatActivity {



        String x;
        TextView testTextView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
             testTextView =(TextView)findViewById(R.id.textView);
            connect();
        }

        public  Connection connect() {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:mysql://"ip-address"/MarcelGerber?user="username"&password="password");
                return conn;
            } catch (Exception e) {
                return null;
            }
        }

        public  String getAllKürzel(Connection conn) {
            if (conn == null) {
                return null;
            }
            Statement st = null;
            ResultSet rs = null;
            try {
                st = conn.createStatement();
                rs = st.executeQuery("SELECT Kürzel FROM Benutzer");
                int length = 0;
                if (rs.last()) {
                    length = rs.getRow();
                    rs.beforeFirst();
                }
                String[] kürzel = new String[length];
                while(rs.next()) {
                    kürzel[rs.getRow() - 1] = rs.getString("Kürzel");
                }

                x=kürzel[1];
                return x;
            } catch (Exception e) {
                return null;
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (Exception e) {
                    }
                }
                if (st != null) {
                    try {
                        st.close();
                    } catch (Exception e) {
                    }
                }
            }



        }

        public void fahrplan (View v,Connection conn){

            testTextView.setText(getAllKürzel(conn));
        }


    }

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="eu.rathenau.fahrgemeinschaft.MainActivity">


        <Button
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:text="@string/fahrplan"
            android:id="@+id/buttonFahrplan"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:onClick="fahrplan"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:text="hallo"
            android:id="@+id/textView"
            android:layout_below="@+id/buttonFahrplan"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="66dp" />
    </RelativeLayout>

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="eu.rathenau.fahrgemeinschaft">

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity"
                android:screenOrientation="portrait">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

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

    </manifest>

日志

    04-22 11:20:39.830 28066-28066/eu.rathenau.fahrgemeinschaft E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: eu.rathenau.fahrgemeinschaft, PID: 28066
                                                                                  java.lang.IllegalStateException: Could not find method fahrplan(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'buttonFahrplan'
                                                                                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:321)
                                                                                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:280)
                                                                                      at android.view.View.performClick(View.java:4856)
                                                                                      at android.view.View$PerformClick.run(View.java:19956)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:211)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5373)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

如果你能帮助我,那就太好了。

farhplan签名错误。应该是

 public void fahrplan (View v){

而不是

 public void fahrplan (View v, Connection conn) {

该方法在运行时使用反射解析。签名必须匹配