简体   繁体   中英

Cannot cast 'android.view.View' to 'com.example.avivyaacobi.contactmanager.Button'

I get an error in this line here:

final Button addBtn = (Button) findViewById(R.id.btnAdd);

My code:

package com.example.avivyaacobi.contactmanager;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TabHost;

public class MainActivity extends Activity {

    EditText nameTxt, phoneTxt, emailTxt, addressTxt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nameTxt = (EditText) findViewById(R.id.txtName);
        phoneTxt = (EditText) findViewById(R.id.txtPhone);
        emailTxt = (EditText) findViewById(R.id.txtEmail);
        addressTxt = (EditText) findViewById(R.id.txtAddress);
        TabHost tabHost = (TabHost) findViewById(R.id.tabHost);

        tabHost.setup();

        TabHost.TabSpec tabSpec= tabHost.newTabSpec("creator");
        tabSpec.setContent(R.id.tabCreator);
        tabSpec.setIndicator("Creator");
        tabHost.addTab(tabSpec);

        tabSpec= tabHost.newTabSpec("list");
        tabSpec.setContent(R.id.tabContactList);
        tabSpec.setIndicator("List");
        tabHost.addTab(tabSpec);

        final Button addBtn = (Button) findViewById(R.id.btnAdd);

        nameTxt.addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence s, int start, int count, int after) {

           }

           @Override
           public void onTextChanged(CharSequence s, int start, int before, int count) {
            addBtn.setEnabled(!nameTxt.getText().toString().trim().isEmpty());

           }

           @Override
           public void afterTextChanged(Editable s) {

           }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, 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);
    }
}

I have function Button:

package com.example.avivyaacobi.contactmanager;

/**
 * Created by AvivYaacobi on 10/1/15.
 */
public class Button {
    private boolean enable;

    public void setEnabled(boolean enabled) {
        this.enable = enabled;
    }
}

I have to say that i'm new with this tool (Android Studio) and Java and i don't know what is the problem.

You are trying to cast a Widget ( Button ) to your custom class (also named Button ). Either rename your custom class to something more indicative of what it does, or use the full package identifier of the button widget, like below.

 final android.widget.Button addBtn = (android.widget.Button) findViewById(R.id.btnAdd);

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