我正在尝试永久保存用户访问过的一些地方。因此,每当我在android studio中运行项目时,都会收到错误消息 “由于:java.lang.ClassCastException:无法将java.lang.Class强制转换为java.util.ArrayList”
这是我的代码:
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.google.android.gms.maps.model.LatLng;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
static ArrayList<String> places= new ArrayList<>();
static ArrayList<LatLng> locations = new ArrayList<>();
static ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.lucifer.memorableplaces", Context.MODE_PRIVATE);
ArrayList<String> latitudes = new ArrayList<>();
ArrayList<String> longitudes = new ArrayList<>();
places.clear();
latitudes.clear();
longitudes.clear();
locations.clear();
try {
places = (ArrayList<String>)ObjectSerializer.deserialize(sharedPreferences.getString("places", ObjectSerializer.serialize(new ArrayList<String>())));
latitudes = (ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("latitudes", ObjectSerializer.serialize(new ArrayList<String>())));
longitudes = (ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("longitudes", ObjectSerializer.serialize(new ArrayList<String>())));
} catch (IOException e) {
e.printStackTrace();
}
if (places.size() > 0 && latitudes.size() > 0 && longitudes.size() > 0) {
if (places.size() == latitudes.size() && latitudes.size() == longitudes.size()) {
for (int i = 0; i < latitudes.size(); i++) {
locations.add(new LatLng(Double.parseDouble(latitudes.get(i)), Double.parseDouble(longitudes.get(i))));
}
}
} else {
places.add("Add a new place...");
locations.add(new LatLng(0, 0));
}
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, places);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
intent.putExtra("placeNumber", i);
startActivity(intent);
}
});
}
}
我得到以下错误:
Process: com.example.lucifer.memorableplaces, PID: 15910
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lucifer.memorableplaces/com.example.lucifer.memorableplaces.MainActivity}: java.lang.ClassCastException: java.lang.Class cannot be cast to java.util.ArrayList
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.util.ArrayList
at com.example.lucifer.memorableplaces.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:5301)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2291)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
错误在 place =(ArrayList)ObjectSerializer.deserialize(sharedPreferences.getString(“ places”,ObjectSerializer.serialize(new ArrayList())));
答案 0 :(得分:0)
我终于得到了答案,该错误是由于我将一个类强制转换为arrayList而引起的。所以我先将类更改为arrayList,然后将其强制转换回arrayList,这解决了我的问题。
这样。在这里,我编写了MainActivity.class
sharedPreferences.edit().putString("places",ObjectSerializer.serialize(MainActivity.class)).apply();
必须以以下方式成为MainActivity.places
sharedPreferences.edit().putString("places",ObjectSerializer.serialize(MainActivity.places)).apply();
所以,在这里,我实际上是在将一个类强制转换为arrayList,这给了我这个错误。希望任何初学者都可以防止此问题出现此错误。
谢谢大家...