android.widget.RelativeLayout无法强制转换为android.widegt.ImageButton

时间:2015-10-02 12:07:57

标签: java android android-layout android-relativelayout android-imagebutton

Heyy我想编写一个即时消息,我想使用ImageButton确认在聊天中发送消息。但是当我尝试使用java.lang.RuntimeException: Unable to start activity ComponentInfo{com.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);
    }}

这里是活动代码

import-module ActiveDirectory
$CSV = import-CSV C:\Users\Administrator\Desktop\All_users_list.csv
$OU = "OU=Vworkspace,DC=remote,DC=sojab0on,DC=nl"
$Users = Get-ADUser -Filter * -SearchBase $OU

foreach ($user in $users) 
{            
 $ADUser = Get-ADUser -Identity $user -Properties * | select SamAccountName
Set-ADUser -Company $($user.Company) -Country $($user.Country) -Department $($user.Department) -OfficePhone $($user.OfficePhone)                      
} 

2 个答案:

答案 0 :(得分:0)

下次尝试用英文撰写您的问题:)这是一个英语论坛。

解决您的问题:您尝试将RelativeLayout强制转换为ImageButton。它可能就在这一行:

    sendbutton = (ImageButton) findViewById(R.id.imagebuutonsend);

检查activity_erik__kartenberg布局,如果imagebuutonsend真的是<Imagebutton .../>而不是<RelativeLayout .../>

答案 1 :(得分:0)

你确定&#34; R.id.imagebuutonsend&#34;是一个imageView?您正在尝试将RelativeLayout强制转换为ImageView。检查你的布局,&#34; R.id.imagebuutonsend&#34;可能是一个RelativeLayout。