我收到了错误。错误和代码低于任何人都可以解决这些错误。
ListView listView = (ListView) findViewById(R.id.ChatlistView);
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(RoomListActivity.this,
R.layout.room_row,
R.id.room,
items);
listView.setAdapter(mArrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView textView = (TextView) view;
String message = "You clicked # " + position + ", which is string: " + textView.getText().toString();
Toast.makeText(RoomListActivity.this, message, Toast.LENGTH_LONG).show();
}
});
room_row.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/room"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/img_room"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/img_room"
android:layout_toRightOf="@+id/img_room"
android:layout_toStartOf="@+id/img_room"
android:gravity="left|center_vertical"
android:textSize="16dp" />
<ImageView
android:id="@+id/img_room"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:src="@drawable/chat" />
</RelativeLayout>
logcat的:
03-08 12:53:50.775: E/AndroidRuntime(716): FATAL EXCEPTION: main
03-08 12:53:50.775: E/AndroidRuntime(716): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
03-08 12:53:50.775: E/AndroidRuntime(716): at com.android.wap2mob.RoomListActivity$1$1.onItemClick(RoomListActivity.java:126)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.widget.AbsListView$1.run(AbsListView.java:3168)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.os.Handler.handleCallback(Handler.java:605)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.os.Handler.dispatchMessage(Handler.java:92)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.os.Looper.loop(Looper.java:137)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-08 12:53:50.775: E/AndroidRuntime(716): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 12:53:50.775: E/AndroidRuntime(716): at java.lang.reflect.Method.invoke(Method.java:511)
03-08 12:53:50.775: E/AndroidRuntime(716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-08 12:53:50.775: E/AndroidRuntime(716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-08 12:53:50.775: E/AndroidRuntime(716): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
03-08 12:53:50.775:E / AndroidRuntime(716):致命异常:主03-08 12:53:50.775:E / AndroidRuntime(716):java.lang.ClassCastException: android.widget.RelativeLayout无法强制转换为 android.widget.TextView 03-08 12:53:50.775:E / AndroidRuntime(716):
ClassCastException 是当您尝试将类从一种类型错误地转换为另一种类型时,Java程序中可能发生的异常。
问题在这里
GenTraversableOnce
试试这种方式
TextView textView = (TextView) view;
答案 1 :(得分:0)
试试这个:
删除此行
TextView textView = (TextView) view;
替换它:
TextView textView = (TextView) view.findViewById(id_here);
答案 2 :(得分:0)
查找OnItemClick中提供的textView by View,
org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/phoenix]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:816)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/phoenix]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
... 6 more
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.addServlet(Ljava/lang/String;Ljavax/servlet/Servlet;)Ljavax/servlet/ServletRegistration$Dynamic;
at org.springframework.boot.context.embedded.ServletRegistrationBean.onStartup(ServletRegistrationBean.java:190)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:225)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:85)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:209)
at org.springframework.boot.contex2016-03-07 23:40:29.048 WARN 71400 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
ded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:55)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5513)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 6 more
将以上代码替换为以下代码
TextView textView = (TextView) view;
答案 3 :(得分:0)
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
当您尝试在代码中将view
对象分配给TextView
时发生,只需将其更改如下:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView textView = (TextView) view.findViewById(Your textview id);
String message = "You clicked # " + position + ", which is string: " + textView.getText().toString();
Toast.makeText(RoomListActivity.this, message, Toast.LENGTH_LONG).show();
}
});