无法捕获EditText字段的输入

时间:2012-03-18 03:54:13

标签: android android-edittext

我有3个EditText字段设置方式如下:

<EditText
   android:id="@+id/evnttitle"
   android:layout_width="200dp"
   android:layout_height="wrap_content"
   android:background="#ffffff"
   android:inputType="textNoSuggestions"
   android:textColor="#000000"
   android:textCursorDrawable="@null" 
   android:layout_marginBottom="20dp">

   <requestFocus />
</EditText>

在我的代码中,他们在onCreate之外设置如下:

EditText titlecap;

然后在我的一个方法中,我有这个:

titlecap = (EditText) findViewById(R.id.evnttitle);

CharSequence titleconv = titlecap.getText();

addAppt(timeconv.toString(), titleconv.toString(), ...

这是我现在尝试过的众多方法之一,因为我似乎无法从edittext字段中获取字符串,我一直得到空指针异常,这个版本是我尝试分配给字符序列的地方。如何将EditTexts中键入的内容作为字符串?

logcat的:

enter image description here

LogCat as text:

FATAL EXCEPTION: main
java.lang.NullPointerException
at w1279057.CW2.CW2Organisor$2.onClick(CW2Organisor.java:88)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

添加了OnCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   setContentView(R.layout.main);

   View createappt = findViewById(R.id.newappt);
   createappt.setOnClickListener(this);
   View delappt = findViewById(R.id.delappt);
   delappt.setOnClickListener(this);

   calendar = (CalendarView) findViewById(R.id.cal);

   appts = new ApptsData(this);



}

完全点击方法:

    @Override
public void onClick(View v) {

    switch (v.getId()) {

    case R.id.newappt:

       final Dialog dialog = new Dialog(this);
       dialog.setContentView(R.layout.poptest);
       dialog.setTitle("Create New Appointment");
       dialog.setCancelable(true);

       Button buttoncancel = (Button) dialog.findViewById(R.id.Button01);
       buttoncancel.setOnClickListener(new OnClickListener() {

        // on click for cancel button
           @Override
           public void onClick(View v) {
               dialog.dismiss();
           }
       });

       Button buttonsave = (Button) dialog.findViewById(R.id.Button02);
       buttonsave.setOnClickListener(new OnClickListener() {

        // on click for save button   
           @Override
            public void onClick(View v) {

               timecap = (EditText) findViewById(R.id.evnttime);

               titlecap = (EditText) findViewById(R.id.evnttitle);

               detcap = (EditText) findViewById(R.id.evntdet);

               CharSequence timeconv = timecap.getText();

               CharSequence titleconv = titlecap.getText();

               CharSequence detconv = detcap.getText();


            cursor = getAppts();
            addAppt(timeconv.toString(), titleconv.toString(),       detconv.toString());
            showAppts(cursor);
            dialog.dismiss();
    }
       });

       dialog.show();
        break;

    case R.id.delappt:
        rmvall();
        break;
    }

}

1 个答案:

答案 0 :(得分:1)

不要制作

  

查看createappt并查看onCreate()

本地的delappt

在它之外创建它们,以便它可以在类中的任何地方访问。

View createappt = null;
View delappt = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    createappt = findViewById(R.id.newappt);
    createappt.setOnClickListener(this);

    delappt = findViewById(R.id.delappt);
    delappt.setOnClickListener(this);

    calendar = (CalendarView) findViewById(R.id.cal);

    appts = new ApptsData(this);
}

添加更新

// on click for save button   
@Override
public void onClick(View v) {

    String strTime = ((EditText) dialog.findViewById(R.id.evnttime)).getText();

String strTitle = ((EditText) dialog.findViewById(R.id.evnttitle)).getText();

String strDet = ((EditText) dialog.findViewById(R.id.evntdet)).getText();

cursor = getAppts();
addAppt(strTime, strTitle, strDet);
showAppts(cursor);
dialog.dismiss();
}