I am having an issue where onclick won't find the method it's supposed to execute. Android tries to call the onClick method public void fahrplan(View v,Connection conn)
but is unable to find it. Details of my code are below, and the error is after that. Please help me determine why android is unable to find the onClick method. Thanks.
Right now there's only one activity, so there's no other code running.
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>
Log
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)
It would be really nice if you could help me.
The signature of farhplan is wrong. It should be
public void fahrplan (View v){
and not
public void fahrplan (View v, Connection conn) {
the method is resolved at runtime using reflection. The signature has to match