Home > Software design >  Could not execute method for android:onClick in Android Studio
Could not execute method for android:onClick in Android Studio

Time:12-01

I'm trying to show a database in android studio using java with listview. My app is crashing and I don't know why. In activity home i put a button( onClick-getData) and a listview. My code is: HomeActivity.java (MainActivity is splasher)

package com.example.acam_try_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.List;
import java.util.Map;

public class HomeActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }
    SimpleAdapter ad;
    public void  GetData(View v)
    {
        ListView lstview=(ListView) findViewById(R.id.listView1);

        List<Map<String,String>> Mydatalist=null;
        ListItem Mydata=new ListItem();
        Mydatalist=Mydata.getlist();
        String [] Fromw= {"City,Id_Of_City"};
        int[] Tow={R.id.City_name,R.id.City_id};
        ad=new SimpleAdapter(HomeActivity.this,Mydatalist,R.layout.listlayout,Fromw,Tow);
        lstview.setAdapter(ad);



    }
}

Connection_to_Cities where i connect the db:

package com.example.acam_try_2;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;

import java.sql.Connection;
import java.sql.DriverManager;

public class Connection_to_Cities {
    Connection_to_Cities connection;
    String ip,port,db,un,pass;

    @SuppressLint("NewApi")
    public Connection_to_Cities conclass(){
        ip="192.168.3.10";
        db="Cities_names";
        un="Stefan";
        pass="stefan";
        port="1433";
        StrictMode.ThreadPolicy tpolicy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(tpolicy);
        Connection con =null;
        String ConnectionURL=null;
        try
        {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            ConnectionURL= "jdbc:jtds:sqlserver://" ip ":" port ";" "databaseName="  db  ";user=" un ";password=" pass ";";
            con= DriverManager.getConnection(ConnectionURL);
        }
        catch (Exception ex)
        {
            Log.e("Error : ", ex.getMessage());
        }
        return (Connection_to_Cities) con;
    }
}

ListItem.java:

package com.example.acam_try_2;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.Connection;
public class ListItem {
    Connection connection;
    String ConnectionResult="";
    Boolean isSuccess=false;

    public List<Map<String,String>>getlist()
    {
        List<Map<String,String>> data= null;
        data= new ArrayList<Map<String,String>>();
        try{
            Connection_to_Cities connection_to_cities= new Connection_to_Cities();
            connection= (Connection) connection_to_cities.conclass();
            if (connection != null)
            {
                String qu= "SELECT * FROM Cities";
                Statement statement=connection.createStatement();
                ResultSet resultSet = statement.executeQuery(qu);
                while(resultSet.next())    {
                    Map<String,String> dtname=new HashMap<String,String>();
                    dtname.put("City_name", resultSet.getString("City_name"));
                    dtname.put("City_id", resultSet.getString("City_id"));
                    data.add(dtname);

                }
                ConnectionResult="Succes";
                isSuccess=true;
                connection.close();

            }
            else{
                ConnectionResult="Failed";
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return data;
    }

}

listlayout.xml:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="65dp"
        tools:layout_editor_absoluteY="65dp">
<!--This is for the city name -->
    <TextView
        android:id="@ id/City_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/teal_200"
        />
    <TextView
        android:id="@ id/City_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/City_name"
        android:textColor="@color/teal_700"/>
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

In AndroidManifest.xml i also wrote:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And the 'good stuff' :

I/art: Background sticky concurrent mark sweep GC freed 2126(255KB) AllocSpace objects, 0(0B) LOS objects, 23% free, 1593KB/2MB, paused 9.701ms total 30.477ms
D/: HostConnection::get() New Host Connection established 0xae833420, tid 6160
I/OpenGLRenderer: Initialized EGL, version 1.4
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/EGL_emulation: eglCreateContext: 0xae834d60: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xae834d60: ver 2 0 (tinfo 0xae839430)
D/OpenGLRenderer: Enabling debug mode 0
D/EGL_emulation: eglMakeCurrent: 0xae834d60: ver 2 0 (tinfo 0xae839430)
D/EGL_emulation: eglMakeCurrent: 0xae834d60: ver 2 0 (tinfo 0xae839430)
D/EGL_emulation: eglMakeCurrent: 0xae834d60: ver 2 0 (tinfo 0xae839430)
D/EGL_emulation: eglMakeCurrent: 0xae834d60: ver 2 0 (tinfo 0xae839430)
D/EGL_emulation: eglMakeCurrent: 0xae834d60: ver 2 0 (tinfo 0xae839430)
D/EGL_emulation: eglMakeCurrent: 0xae834d60: ver 2 0 (tinfo 0xae839430)
D/EGL_emulation: eglMakeCurrent: 0xae834d60: ver 2 0 (tinfo 0xae839430)
D/EGL_emulation: eglMakeCurrent: 0xae834d60: ver 2 0 (tinfo 0xae839430)
D/EGL_emulation: eglMakeCurrent: 0xae834d60: ver 2 0 (tinfo 0xae839430)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.acam_try_2, PID: 6137
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446)
        at android.view.View.performClick(View.java:4780)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        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:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
        at android.view.View.performClick(View.java:4780) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
        at android.view.View$PerformClick.run(View.java:19866) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        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:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
     Caused by: java.lang.ClassCastException: net.sourceforge.jtds.jdbc.JtdsConnection cannot be cast to com.example.acam_try_2.Connection_to_Cities
        at com.example.acam_try_2.Connection_to_Cities.conclass(Connection_to_Cities.java:35)
        at com.example.acam_try_2.ListItem.getlist(ListItem.java:22)
        at com.example.acam_try_2.HomeActivity.GetData(HomeActivity.java:28)
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) 
        at android.view.View.performClick(View.java:4780) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
        at android.view.View$PerformClick.run(View.java:19866) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        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:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Disconnected from the target VM, address: 'localhost:63653', transport: 'socket'

Is my first time when i do this kind of thing and i really don't know why it's happening :((

CodePudding user response:

Your problem is actually toward the bottom of the stack trace:

Caused by: java.lang.ClassCastException: net.sourceforge.jtds.jdbc.JtdsConnection cannot be cast to com.example.acam_try_2.Connection_to_Cities

Look for this cast problem somewhere in the code executed within the onClick listener. return (Connection_to_Cities) con looks like a good place to start.

  • Related