android.widget.RelativeLayout 无法转换为 android.widget.ImageButton
Posted
技术标签:
【中文标题】android.widget.RelativeLayout 无法转换为 android.widget.ImageButton【英文标题】:android.widget.RelativeLayout cannot be cast to android.widegt.ImageButton 【发布时间】:2015-12-30 15:30:14 【问题描述】:嘿,我想编写一个即时通讯程序,我想使用ImageButton
来确认在聊天中发送消息。但是当我尝试使用ImageButton
时,我遇到了以下问题:
java.lang.RuntimeException: Unable to start activity ComponentInfocom.secmess.secmess/com.secmess.secmess.Erik_Kartenberg: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.ImageButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
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: android.widget.RelativeLayout cannot be cast to android.widget.ImageButton
at com.secmess.secmess.Erik_Kartenberg.onCreate(Erik_Kartenberg.java:23)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
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)
10-02 13:50:38.717 16820-16820/? I/Process﹕ Sending signal. PID: 16820 SIG: 9
这里是活动代码
package com.secmess.secmess;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class Erik_Kartenberg extends Activity
private EditText nachricht;
private TextView textout;
private ImageButton sendbutton;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_erik__kartenberg);
sendbutton = (ImageButton) findViewById(R.id.imagebuutonsend);
sendbutton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
nachricht = (EditText) findViewById(R.id.editTextwithhint);
textout = (TextView) findViewById(R.id.textoutviewer);
textout.setText(nachricht.getText());
nachricht.setText("");
);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_erik__kartenberg, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
【问题讨论】:
添加你的布局文件 请用英文提问! 哦,好吧,对不起。我想我可以用德语问 发布您的 layout.xml 文件 【参考方案1】:下次尝试用英文写你的问题:) 这里是一个英文论坛。
对于您的问题:您尝试将 RelativeLayout 转换为 ImageButton。大概就在这一行:
sendbutton = (ImageButton) findViewById(R.id.imagebuutonsend);
检查您的activity_erik__kartenberg
布局,如果imagebuutonsend
真的是<Imagebutton .../>
而不是<RelativeLayout .../>
。
【讨论】:
【参考方案2】:您确定“R.id.imagebuutonsend”是一个 imageView 吗?您正在尝试将 RelativeLayout 转换为 ImageView。检查你的布局,“R.id.imagebuutonsend”可能是一个RelativeLayout。
【讨论】:
以上是关于android.widget.RelativeLayout 无法转换为 android.widget.ImageButton的主要内容,如果未能解决你的问题,请参考以下文章