gpt4 book ai didi

java - Android - 将类 X 的实例从 Activity A 发送到 Activity B

转载 作者:行者123 更新时间:2023-11-30 01:45:01 25 4
gpt4 key购买 nike

我试图从我的 Mainactivity 将我的 Connection 类发送到我的 Mapsactivity,这样我就可以在我的 Mapsactivity 的 Connection 类中使用方法。

由于某种原因,我在启动 Mapsactivity 时遇到此错误,

java.lang.RuntimeException: Parcelable 遇到IOException 写入可序列化对象

我不太确定问题出在哪里,我的 Connection 类已经实现了 Serializable,我也让我的 Mainactivity 实现了 Serializable 以查看它是否会修复但没有成功。

我的应用程序应该让人们连接到一个服务器,他们可以在服务器上发送他们的位置,并从其他连接的人那里接收位置,并将坐标放在他们的 map 上。我已经删除了与此问题无关的代码,因此可以阅读的代码更少。我相信我已经获得了所有必要的代码,如果还不够,我可以发布整个类(class)。提前致谢

主要 Activity 类

public class MainActivity extends FragmentActivity{
private Connection connection;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
setListeners();
}

public void initialize() {
connection = new Connection("xxx.xxx.xxx.x",xxxx);
btnShowMap = (Button)findViewById(R.id.btnShowMap);
Log.w(null, "Initialized");
}

public void startMap() {
Intent intent = new Intent(this, MapsActivity.class);
intent.putExtra("class", connection);
startActivity(intent);
}

public void setListeners() {
btnShowMap.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btnShowMap.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startMap();
}
});
}
});
}

连接类

    public class Connection implements Serializable{
private InetAddress address;
private int port;
private String ip, groupName, myName, total, membersInfo, id, fName;
private Socket socket;
private InputStream is;
private DataInputStream dis;
private OutputStream os;
private DataOutputStream dos;
private boolean connected = false;
private double latitude, longitude, fLong ,fLat;
private String name;
private double Longi, Lati;

public Connection(String ip, int port){
this.ip = ip;
this.port = port;
}

public void listenToServer() {
Thread thread = new Thread(new ListenToServer());
thread.start();
}

private class ListenToServer implements Runnable {
public void run() {
Log.w(null,"Listening to server");
try {
String locations = new String(dis.readUTF());
if(locations == null) {
Log.e(null,"EMPTY");
}
if(locations != null) {
JSONObject friends = new JSONObject(locations);
JSONArray jsonArray = friends.getJSONArray("location");
JSONObject transaction;
for (int i = 0; i < jsonArray.length(); i++) {
transaction = jsonArray.getJSONObject(i);
fName = transaction.getString("member");
fLong = transaction.getDouble("longitude");
fLat = transaction.getDouble("latitude");
}
}
if (locations == null) {
dis.close();
}
}
catch (JSONException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}

MapsActivity 类

public class MapsActivity extends FragmentActivity {

private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LocationManager locationManager;
private LocationListener locationListener;
private double latitude, longitude, flat, flong;
private String friendName;
private boolean addMarker = false;
private Connection connection;
private Button btnAddMyMarker, btnAddFriendMarker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
initialize();
setListener();
}

public void initialize() {
btnAddMyMarker = (Button)findViewById(R.id.btnAddMyMarker);
btnAddMyMarker.setEnabled(false);
btnAddFriendMarker = (Button)findViewById(R.id.btnAddFriendMarker);
connection = (Connection) getIntent().getSerializableExtra("class");
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocList();

}

public void setListener() {
btnAddMyMarker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addMyMarker();
}
});

btnAddFriendMarker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
connection.listenToServer();
}
});
}


public void addMyMarker() {
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("My position"));
}

private class LocList implements LocationListener {
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
if(addMarker == false && longitude != 0 && latitude != 0) {
btnAddMyMarker.setEnabled(true);
}
connection.sendLocation(latitude, longitude);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}

最佳答案

您将无法在 Activity 之间传递 Connection,因为其中的大部分内容无法序列化或转换为 Parcel。值得注意的是,以下都不支持任一操作:

private InputStream is;
private DataInputStream dis;
private OutputStream os;
private DataOutputStream dos;

或者:

  • 这应该是一项服务,特别是如果工作应该独立于任何 UI 完成,或者

  • 这需要是单例,或者

  • 没有单独的 Activity ,而是一个 Activity 有多个 fragment ,或者

  • 将所有与连接相关的工作隔离到一个 Activity 中

关于java - Android - 将类 X 的实例从 Activity A 发送到 Activity B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33860815/

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