2016-04-22 101 views
-2

我有一个问题,onclick不会找到它应该exexute的方法。 Android尝试调用onClick方法public void fahrplan(View v,Connection conn),但无法找到它。我的代码细节如下,错误是在那之后。请帮我确定为什么android无法找到onClick方法。谢谢。android - onclick找不到方法

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

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) 

如果你能帮助我,这将是非常好的。

回答

1

farhplan的签名是错误的。它应该是

public void fahrplan (View v){ 

,而不是

public void fahrplan (View v, Connection conn) { 

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

+0

不,它必须是'public void fahrplan(View v,Connection conn){'。如果不是android/java不能解析符号“conn” –

+0

它可以是任何你想要的。这并不意味着它就是运行时环境期望的结果 – Blackbelt

+0

实际的问题是'public void fahrplan(View v,Connection conn){'从不使用。 –