android.support.v7.widget.AppCompatEditText {...}

时间:2017-11-22 00:27:49

标签: java android-studio android-appcompat

我最近开始在android studio工作,我想在添加任何SQL之前创建应用程序的UI,因为我不想学习一个不能工作的程序的语言,但是方式,我尝试使用带有文本框的TableLayout,并获取用户输入并将其插入到该表中。这是行不通的。我一直收到图片中显示的错误。[错误显示] [1]

package com.example.tj_n126.firstappattempt;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }
    };

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

        TextView lastNameDisplay = findViewById(R.id.lNameTxt);
        TextView firstNameDisplay = findViewById(R.id.nameTxt);
        TextView phoneNumDisplay = findViewById(R.id.phoneTxt);
        TextView emailDisplay = findViewById(R.id.emailTxt);
        TextView accBalDisplay = findViewById(R.id.accBal);

        firstNameDisplay.setText("First");
        lastNameDisplay.setText("Last");
        phoneNumDisplay.setText("Phone");
        emailDisplay.setText("Email");
        accBalDisplay.setText("Balance");

        final Button newUserButton = findViewById(R.id.newUserButton);
        newUserButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //this is for inputs on a separate page
                setContentView(R.layout.layout2);
                final TextView lastName = findViewById(R.id.lastName);
                final TextView firstName = findViewById(R.id.firstName);
                final TextView phoneNumber = findViewById(R.id.phoneNumber);
                final TextView email = findViewById(R.id.emailAddress);
                final TextView accountBalance = findViewById(R.id.accountAmount);
                Button submitBtn = findViewById(R.id.submitBtn);
                submitBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        setContentView(R.layout.activity_main);

                        //These are the textboxes in the table
                        TextView fName = findViewById(R.id.fName);
                        TextView lName = findViewById(R.id.lName);
                        TextView phone = findViewById(R.id.phone);
                        TextView emailAdd = findViewById(R.id.email);
                        TextView balance = findViewById(R.id.Balance);

                        fName.setText(firstName + "");
                        lName.setText(lastName + "");
                        phone.setText(phoneNumber + "");
                        emailAdd.setText(email + "");
                        balance.setText(accountBalance + "");
                    }
                });
            }
        });

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }
}


  [1]: https://i.stack.imgur.com/8CfWD.png

1 个答案:

答案 0 :(得分:0)

在你的代码的这一行:

fName.setText(firstName + "");

变量firstName实际上是一个TextView,因此编译器将隐式调用firstName.toString(),它将为您提供您看到的字符串(它不会在TextView中为您提供文本)。该行应该是:

fName.setText(firstName.getText());

同样适用于使用TextView + ""模式的其他地方(由于任何原因我不鼓励使用)。