【问题标题】:Input and printing from EditText从 EditText 输入和打印
【发布时间】:2014-09-11 04:14:06
【问题描述】:

所以我试图从 EditText 视图中获取一个字符串并将其存储到一个变量中,然后在单击 CreateBtn 时将其打印出来。不能完全让它工作。我认为,由于 CreateBtn 不断崩溃,我认为这可能与 EditText 视图所在的 AlertBox 有关。

public class MyActivity extends Activity {


// declares vars
final Context context = this;
private Button addClassBtn;
private Button createBtn;
public EditText classNameInput;
private String classTextName;

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

    // initializes var
    final LayoutInflater classInfo = getLayoutInflater();
    addClassBtn = (Button) findViewById(R.id.classBtn);
    classNameInput = (EditText) findViewById(R.id.classNameInput);
    createBtn = (Button) findViewById(R.id.createBtn);

    // creates a alert box with a text view when button is clicked
    addClassBtn.setOnClickListener(
        new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder builder = new AlertDialog.Builder(context);

            builder.setView(classInfo.inflate(R.layout.class_info, null));

            builder.setTitle("Add Class");

            AlertDialog diaBox = builder.create();

            diaBox.show();

        }
    });

    //classNameInput is extracted to a variable
    createBtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String className;

                    className = classNameInput.getText().toString();

                    Log.v("EditText", className);

                }
            });

这里是class_info.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
    android:id="@+id/classNameInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:paddingTop="25dp"
    android:hint="    class name    "
    android:textSize="25sp"
    android:paddingBottom="20dp"
    android:fontFamily="monospace"
    android:inputType="textAutoComplete|textAutoCorrect"
    android:textAlignment="center"
    android:paddingLeft="-10dp"
    android:paddingRight="-10dp" />

<Button
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/classNameInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/createBtn"
    android:fontFamily="monospace"
    android:textSize="23sp"
    android:text="create"
    android:onClick="createBtnClick" />

</RelativeLayout>

这里是activity_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/classBtn"
    android:clickable="false"
    android:textSize="100sp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="onClassBtnClick"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:focusable="true" />

 </RelativeLayout>

【问题讨论】:

标签: java android


【解决方案1】:

我认为当您按下 createBtn 时出现 NullPointerException,因为当您尝试在 createBtn 的 OnClickListener 中使用您的 classNameInput 时为空。

语句“(EditText) findViewById(R.id.classNameInput)”总是返回 null,因为您的 EditText 位于使用该语句时尚未创建的 AlterDialog 中。

为了解决您的问题,我需要知道您的 createBtn 在哪里。最好发布你的activity_my xml和class_info xml。

更新:

根据您发布的布局文件,我知道您在启动 MyActivity 时立即收到 NullPointerException,因为当您为其设置 OnClickListener 时,您的 createBtn 为空。原因与我所说的关于您的 classNameInput 的原因相同。

我更改了您的部分代码以解决您的问题。以下代码应该可以解决您的问题:

public class MyActivity extends Activity {


// declares vars
final Context context = this;
private Button addClassBtn;
private Button createBtn;
public EditText classNameInput;
private String classTextName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
       // initializes var
    final LayoutInflater classInfo = getLayoutInflater();
    addClassBtn = (Button) findViewById(R.id.classBtn);

    // creates a alert box with a text view when button is clicked
    addClassBtn.setOnClickListener(
        new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder builder = new AlertDialog.Builder(context);

            View dialogView = classInfo.inflate(R.layout.class_info, null);

            setDialogViews(dialogView);

            builder.setView(dialogView);

            builder.setTitle("Add Class");

            AlertDialog diaBox = builder.create();

            diaBox.show();

        }
    });


    private void setDialogViews(View dialogView){
        classNameInput = (EditText) dialogView.findViewById(R.id.classNameInput);
        createBtn = (Button) dialogView.findViewById(R.id.createBtn);

        //classNameInput is extracted to a variable
        createBtn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String className;

                        className = classNameInput.getText().toString();

                        Log.v("EditText", className);

                    }
                });
    }

【讨论】:

  • 非常感谢!只是想知道为什么我用创建按钮和编辑文本字段指向一个空指针?
  • @waffles 如果您想使用 findViewById() 查找视图,则必须确保在正确的视图树上调用该方法。在 Activity 中使用 findViewById() 方法意味着您希望在 Activity 的内容视图树上找到它,但是您的“创建按钮”和“编辑文本”都不在该视图树上,因此该方法返回 null。其实你的Button和EditText就在你对话框的View Tree上,所以使用dialogView.findViewById()就可以找到了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
相关资源
最近更新 更多