gpt4 book ai didi

Java - java.io.NotSerializableException : net. sourceforge.jtds.jdbc.JtdsConnection

转载 作者:行者123 更新时间:2023-11-29 02:25:57 27 4
gpt4 key购买 nike

我想分享一个类(class)的 Activity 。我尝试了一切,到处搜索,但无法解决我的问题。最重要的是我必须使用 BusinessLogic 作为整个应用程序的控制类。每个变量、方法都在其中。有人可以帮我吗?

public void ConnectSQL() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.3.10;databaseName=hungary;user=stockiduser;password=Berwin1559;loginTimeout=5;socketTimeout=5");
KapcsolatAllasKijelzes(false);
} catch (Exception e) {
KapcsolatAllasKijelzes(true);
}
}

此方法位于我名为 BusinessLogic 的类中...这是 MainActivity 代码:

Intent intent=new Intent(this, Kiszedes.class);
intent.putExtra("bl",bl);
startActivity(intent);

Kiszedes.class 是目标 Activity :

 BusinessLogic bl = (BusinessLogic) getIntent().getSerializableExtra("bl");

这是 LogCat 所说的:

proba.diszpo E/AndroidRuntime: FATAL EXCEPTION: main
Process: proba.diszpo, PID: 11721
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = proba.diszpo.BusinessLogic)
at android.os.Parcel.writeSerializable(Parcel.java:1526)
at android.os.Parcel.writeValue(Parcel.java:1474)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:723)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1133)
at android.os.Parcel.writeBundle(Parcel.java:763)
at android.content.Intent.writeToParcel(Intent.java:8660)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3154)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4226)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
at android.app.Activity.startActivityForResult(Activity.java:4185)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4509)
at android.app.Activity.startActivity(Activity.java:4477)
at proba.diszpo.MainActivity.onClick(MainActivity.java:177)
at android.view.View.performClick(View.java:5675)
at android.view.View$PerformClick.run(View.java:22641)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.io.NotSerializableException: net.sourceforge.jtds.jdbc.JtdsConnection
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at android.os.Parcel.writeSerializable(Parcel.java:1521)
at android.os.Parcel.writeValue(Parcel.java:1474)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:723)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1133)
at android.os.Parcel.writeBundle(Parcel.java:763)
at android.content.Intent.writeToParcel(Intent.java:8660)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3154)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4226)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
at android.app.Activity.startActivityForResult(Activity.java:4185)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4509)
at android.app.Activity.startActivity(Activity.java:4477)
at proba.diszpo.MainActivity.onClick(MainActivity.java:177)
at android.view.View.performClick(View.java:5675)
at android.view.View$PerformClick.run(View.java:22641)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

最佳答案

您的 BusinessLogic 类包含对 JtdsConnection 对象的引用,该对象不可序列化。 BusinessLogicSerializable 是不够的,它的所有成员(以及成员的成员等)。也需要 Serializable

由于JtdsConnection 不是您创建的类,因此您不能使其Serializable。无论如何,您尝试完成这项工作的方式并不正确。


我建议为您的业务逻辑创建一个全局实例,这样您就可以从任何 Activity/fragment 访问它。

在我的项目中,我通常创建一个 Conf 类,其中包含我需要的对象的 static 实例。在你的情况下你可以有这样的东西:

public class Conf {

@Nullable
private static BusinessLogic businessLogic;

@NonNull
public static BusinessLogic getBusinessLogic() {

if (businessLogic == null) {
businessLogic = new BusinessLogic();
}

return businessLogic;
}
}

在您的 Activity 中,您现在可以简单地执行以下操作:BusinessLogic bl = Conf.getBusinessLogic()

关于Java - java.io.NotSerializableException : net. sourceforge.jtds.jdbc.JtdsConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201875/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com