我写的过滤器抛出了ClassCastException
[Ljava.security.cert.X509Certificate; 无法转换为java.security.cert.X509Certificate
当我试图转换从ServletRequest属性中提取的Object时,即
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws
IOException, ServletException
{
X509Certificate cert = (X509Certificate) req.getAttribute("javax.servlet.request.X509Certificate");
System.out.println("cert dn " + cert.getSubjectDN().toString());
filterChain.doFilter(req, res);
}
Run Code Online (Sandbox Code Playgroud)
当我深入挖掘时,我理解像这样的异常很可能是由不同的类加载器引起的,尽管它们属于同一类类型.我该如何解决这个问题?
谢谢
我使用以下Spring 3配置来加载Jetty 7零碎
<bean class="org.eclipse.jetty.server.Server"
init-method="start" destroy-method="stop">
<property name="connectors">
<list>
<bean id="SSLConnector" class="org.eclipse.jetty.server.ssl.SslSocketConnector">
<property name="port" value="8553"/>
<property name="maxIdleTime" value="3600000"/>
<property name="soLingerTime" value="-1"/>
<property name="needClientAuth" value="true"/>
<property name="sslContext">
<ref bean="sslContext"/>
</property>
</bean>
</list>
</property>
<property name="handler">
<bean name="contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection">
<property name="handlers">
<list>
<bean class="org.eclipse.jetty.servlet.ServletContextHandler">
<property name="contextPath">
<value>/caas</value>
</property>
<property name="resourceBase" …Run Code Online (Sandbox Code Playgroud) 假设我有扩展SuperClass的类SubClass1和SubClass2.我需要将所有扩展SuperClass的对象存储在列表中(列表列表).现在我将列表传递给一个函数,该函数可以确定SuperClass之前的SubClass.然后,此函数应将SuperClass重新转换回其各自的子类.例如,
for (SuperClass superClass : list) {
if (superClass.getType().equals("SubClass1")
SubClass1 subClass = (SubClass1) superClass;
else if (superClass.getType().equals("SubClass2")
SubClass2 subClass = (SubClass2) superClass;
}
Run Code Online (Sandbox Code Playgroud)
此示例将导致类强制转换异常.是否有任何简单的解决方案来获得此功能?
编辑:正如其中一个答案所述,此代码应该导致异常.在此示例中,getType()方法必定存在问题.但是,使用instanceof关键字的解决方案通过消除对getType()方法的需要来解决问题.
我正在尝试使用这篇文章的解决方案,以便在我的ActionBar中有一个Spinner.我第一次使用ActionBar的NAVIGATION_MODE_LIST,但我不希望使用微调器来浏览槽视图(我将有标签).所以我创建了2 xml:
mode_spinner.xml
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
options.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_mode"
android:actionLayout="@layout/mode_spinner"
android:showAsAction="ifRoom"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
然后,试图从我的片段中膨胀它(SherlockFragment)
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.options, menu);
Spinner spinner = (Spinner) menu.findItem(R.id.menu_mode);
spinner.setAdapter(mSpinnerAdapter);
super.onCreateOptionsMenu(menu, inflater);
}
Run Code Online (Sandbox Code Playgroud)
在运行时,我收到此错误:
java.lang.ClassCastException:com.actionbarsherlock.internal.view.menu.MenuItemWrapper无法强制转换为android.widget.Spinner
任何的想法 ?
android classcastexception actionbarsherlock android-actionbar
我使用ArrayList来存储一些字符串数组的字符串:
ArrayList<String> list = new ArrayList<>();
list.add(strings[i]);
Run Code Online (Sandbox Code Playgroud)
为了将这些函数转换为另一个函数,我将它们转换回一个数组:
foo((String[]) list.toArray());
Run Code Online (Sandbox Code Playgroud)
这给了我一个例外:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
我觉得愚蠢问这个问题,但我真的没有看到问题.编译它给了我ClassCastExeption
这是超级课程
package elements;
import persistence.postgres.DataSource;
import persistence.postgres.IdBroker;
import persistence.postgres.PersistenceException;
import util.Util;
public class Hotspot {
private double lat;
private double lng;
private long id;
private String username;
private String time_stamp;
private String point_of_interest;
private String comment;
public Hotspot(double lat, double lng, String username, String comment, String p_o_i) {
this.lat = lat;
this.lng = lng;
this.username = username;
try {
this.id = IdBroker.getId(new DataSource().getConnection());
} catch (PersistenceException e) {
e.printStackTrace();
}
time_stamp = Util.timeStamp();
this.comment = comment;
this.point_of_interest = p_o_i; …Run Code Online (Sandbox Code Playgroud) 我开发了一个简单的listview应用程序,它显示了listview,但是当我选择单个列表视图时,它会出现以下错误.
这是我的单个列表项xml文件.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/product_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:padding="10dip"
android:textColor="#ffffff"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是我的MainActivity.java文件
import java.util.List;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity implements FetchDataListener{
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将字符串转换为日期:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Date dataEvento = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dataEvento = (Date)dateFormat.parse("28/08/2010"); //the app crash here
Run Code Online (Sandbox Code Playgroud)
logcat的:
03-06 10:06:19.867: E/AndroidRuntime(426): FATAL EXCEPTION: main
03-06 10:06:19.867: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ladessa.jccb/com.ladessa.jccb.MainActivity}: java.lang.ClassCastException: java.util.Date
03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.os.Looper.loop(Looper.java:123) 03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-06 10:06:19.867: E/AndroidRuntime(426): at java.lang.reflect.Method.invokeNative(Native …Run Code Online (Sandbox Code Playgroud) 使用 Gson 将 JSON 字符串转换为 Java 对象时,我收到此警告。为什么我得到它,我该如何解决它?
这是我在代码中收到的警告:
Unchecked assignment:
'net.brawtasports.brawtasportsgps.model.JSONKeys' to
'net.brawtasports.brawtasportsgps.model.JSONKeys<net.brawtasports.brawtasportsgps.model.team.Object>'
Run Code Online (Sandbox Code Playgroud)
这是我在运行时得到的错误:
java.lang.ClassCastException:
com.google.gson.internal.LinkedTreeMap cannot be cast to
net.brawtasports.brawtasportsgps.model.team.Object
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
String jsonString = Preferences.readFromPreferences(ApplicationConstants.team_data,"");
Gson gson = new Gson();
JSONKeys<Object> teamMembers = gson.fromJson(jsonString, JSONKeys.class); //converts json string to java object
Object players = teamMembers.getObject();//object is my custom class
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,players);
ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_dropdown_item,players.getPlayersSummary());
player1.setAdapter(arrayAdapter);
Run Code Online (Sandbox Code Playgroud)
这是我的 JSONKeys POJO 的代码:
public class JSONKeys<T> {
private boolean Success;
private String Message;
private int ObjectIdentifier;
private T …Run Code Online (Sandbox Code Playgroud) 我有这个错误.这是我的代码:
GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME, null, null){
@Override
public void setUserCredentials(String username, String password)
throws AuthenticationException {
// Nothing to do here, just Overriding the old method and setting it to null so we can later setOauthCredentials to the service
}};
service.setOAuth2Credentials(credential);
List users = new ArrayList();
for (int i = 0; i < emailsData.size(); i++)
{
users.add(emailsData.get(0).get(i).split("@"));
String signature = "some html code";
String escaped = StringEscapeUtils.escapeHtml4(signature).toString();
service.changeSignature(users, escaped);
users.remove(0);
}
Run Code Online (Sandbox Code Playgroud)
IDE将我发送到service.changeSignature(users, escaped);下一个例外:
Exception in …Run Code Online (Sandbox Code Playgroud) 尝试使用json.simple库解析以下JSON字符串时:
[
{"id" : "6d7662a9.f8ba04"},
{"id" : "2da98cc2.145ba4"},
{"id" : "45492640.a17d68"}
]
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONArray
Run Code Online (Sandbox Code Playgroud)
这就是我的做法:
JSONArray json = (JSONArray) new JSONParser().parse(jsonString);
Run Code Online (Sandbox Code Playgroud)
JSON字符串是一个数组,因此不确定为什么会引发该异常。
这里有几个类似的问题,但是在他们的情况下,他们试图将a强制转换JSONObject为a,JSONArray因此抛出异常是有意义的,但在这种情况下,它看起来是正确的。
-----------------编辑-----------------
我添加了一行来打印对象的类,如下所示:
Object json = new JSONParser().parse(jsonString);
System.out.println(json.getClass());
Run Code Online (Sandbox Code Playgroud)
打印以下行:
class org.json.simple.JSONArray
Run Code Online (Sandbox Code Playgroud)
在下一行中,如果出现以下情况,则显示为:
if(json instanceof JSONArray) {
System.out.println("This is a JSONArray");
}
Run Code Online (Sandbox Code Playgroud)
但是它不访问if,所以它真的很奇怪,因为首先我检查对象是否为JSONArray,但它不会打印 "This is a JSONArray"