简体   繁体   中英

android.widget.RelativeLayout cannot be cast to android.widegt.ImageButton

Heyy I want to program an instant messenger and i want to use an ImageButton to confirm sending the Message in the Chat. But when i try to use the ImageButton i get following issue:

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

Here the Activity-Code

    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);
    }}

Next time try to write your question in english :) Its an english forum here.

To your problem: You try to cast a RelativeLayout into an ImageButton. It's probably in this line:

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

Check in your activity_erik__kartenberg layout, if imagebuutonsend is really an <Imagebutton .../> and not a <RelativeLayout .../> .

Are you sure "R.id.imagebuutonsend" is an imageView? You are trying to cast RelativeLayout into ImageView. Check your layout, "R.id.imagebuutonsend" is probably a RelativeLayout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM