簡體   English   中英

android.widget.RelativeLayout無法轉換為android.widegt.ImageButton

[英]android.widget.RelativeLayout cannot be cast to android.widegt.ImageButton

嘿,我想編寫一個即時通訊程序,我想使用ImageButton確認在聊天中發送消息。 但是當我嘗試使用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);
    }}

下次嘗試用英語寫您的問題:)這是一個英語論壇。

對於您的問題:您嘗試將RelativeLayout轉換為ImageButton。 可能在這一行:

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

檢查您的activity_erik__kartenberg布局,如果imagebuutonsend實際上是<Imagebutton .../>而不是<RelativeLayout .../>

您確定“ R.id.imagebuutonsend”是imageView嗎? 您正在嘗試將RelativeLayout投射到ImageView中。 檢查您的布局,“ R.id.imagebuutonsend”可能是RelativeLayout。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM