java无法在安卓 Studio中为安卓:onClick执行方法
我试图在安卓 studio中使用java和listview显示一个数据库。我的应用程序正在崩溃,我不知道为什么。在活动主页中,我放置了一个按钮(onClick getData)和一个列表视图。 我的代码是: 家庭活动。java(主要活动是splasher)
package com.example.acam_try_2;
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.ListView;
import 安卓.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);
}
}
连接到我连接db的城市:
package com.example.acam_try_2;
import 安卓.annotation.SuppressLint;
import 安卓.os.StrictMode;
import 安卓.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;
}
}
列表项。爪哇:
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;
}
}
列表布局。xml:
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
xmlns:tools="http://schemas.安卓.com/tools">
<RelativeLayout
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:layout_editor_absoluteX="65dp"
tools:layout_editor_absoluteY="65dp">
<!--This is for the city name -->
<TextView
安卓:id="@+id/City_name"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textColor="@color/teal_200"
/>
<TextView
安卓:id="@+id/City_id"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_toRightOf="@id/City_name"
安卓:textColor="@color/teal_700"/>
</RelativeLayout>
</安卓x.constraintlayout.widget.ConstraintLayout>
在AndroidManifest中。我还写了:
<uses-permission 安卓:name="安卓.permission.INTERNET" />
<uses-permission 安卓:name="安卓.permission.ACCESS_NETWORK_STATE" />
还有“好东西”:
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 安卓:onClick
at 安卓x.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446)
at 安卓.view.View.performClick(View.java:4780)
at com.google.安卓.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at 安卓.view.View$PerformClick.run(View.java:19866)
at 安卓.os.Handler.handleCallback(Handler.java:739)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:135)
at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.安卓.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 安卓x.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
at 安卓.view.View.performClick(View.java:4780)
at com.google.安卓.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at 安卓.view.View$PerformClick.run(View.java:19866)
at 安卓.os.Handler.handleCallback(Handler.java:739)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:135)
at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.安卓.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 安卓x.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
at 安卓.view.View.performClick(View.java:4780)
at com.google.安卓.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at 安卓.view.View$PerformClick.run(View.java:19866)
at 安卓.os.Handler.handleCallback(Handler.java:739)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:135)
at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Disconnected from the target VM, address: 'localhost:63653', transport: 'socket'
这是我第一次做这种事情,我真的不知道为什么会这样:((
# 1 楼答案
您的问题实际上是在堆栈跟踪的底部:
在onClick监听器中执行的代码中查找这个强制转换问题
return (Connection_to_Cities) con
看起来是个不错的开始