我正在嘗試使用帶有串列視圖的 java 在 android studio 中顯示一個資料庫。我的應用程式崩潰了,我不知道為什么。在活動主頁中,我放了一個按鈕(onClick-getData)和一個串列視圖。我的代碼是: 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 我連接資料庫的地方:
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;
}
}
串列布局.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>
在 AndroidManifest.xml 我還寫道:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.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 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'
這是我第一次做這種事情,我真的不知道為什么會這樣:((
uj5u.com熱心網友回復:
您的問題實際上是在堆疊跟蹤的底部:
引起:java.lang.ClassCastException:net.sourceforge.jtds.jdbc.JtdsConnection 無法轉換為 com.example.acam_try_2.Connection_to_Cities
在onClick偵聽器中執行的代碼中的某處查找此強制轉換問題。return (Connection_to_Cities) con看起來是個不錯的起點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/373528.html
