无法从edittext获取价值

时间:2012-07-15 12:42:35

标签: android

我有以下代码。

final Button Submit= (Button) findViewById(R.id.SubmitButton);
            Submit.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {

                chatbox=(EditText)findViewById(R.id.Chatbox);   <-- error
                String data=chatbox.getText().toString();
                Log.i("chama",data);

                HashMap<String,String> hashMap=new HashMap<String,String>();    
                        hashMap.put("You",chatbox.getText().toString());    //insert username(You) value in chatbox to hashmap

                        adminChat.add(hashMap);                                 //insert hashMap in arraylist
                        ShowListView(adminChat);                            //pass it to listview

                        sendHttpRequest("set", "admin", "You" ,chatbox.getText().toString(),clientEmail,UserAdminChatActivity.LastShowingChatDate);                   

         }});

它在xml中声明如此

<EditText
    android:id="@+id/Chatbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@id/SubmitButton"
    android:layout_alignBottom="@id/SubmitButton"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@id/SubmitButton"
    android:ems="10"
    android:inputType="text" >
</EditText>

logcat出现以下错误:

07-15 17:40:20.083: E/AndroidRuntime(5503): FATAL EXCEPTION: main
07-15 17:40:20.083: E/AndroidRuntime(5503): java.lang.NullPointerException
07-15 17:40:20.083: E/AndroidRuntime(5503):     at com.app.ServerClient.UserAdminChatActivity$2.onClick(UserAdminChatActivity.java:52)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at android.view.View.performClick(View.java:2485)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at android.view.View$PerformClick.run(View.java:9080)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at android.os.Handler.handleCallback(Handler.java:587)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at android.os.Looper.loop(Looper.java:123)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at android.app.ActivityThread.main(ActivityThread.java:3647)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at java.lang.reflect.Method.invoke(Method.java:507)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-15 17:40:20.083: E/AndroidRuntime(5503):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

试试这个:

String data=""; 
chatbox=(EditText)findViewById(R.id.Chatbox);
if(chatbox.getText().toString()!=null)
    data=chatbox.getText().toString();

另外,请确保您引用的EditText与用于通过setContentView()

的活动进行映射的xml文件位于同一个文件中

答案 1 :(得分:1)

你应该这样做

chatbox= (EditText) findViewById(R.id.Chatbox);

onClick方法之外。这对我来说没有任何意义。在您设置widget后,应立即初始化contentView

所以

...
protected EditText chatbox;

@Override
public void onCreate(Bundle savedOnInstanceState) {
   super.onCreate(savedOnInstanceState);
   setContentView(R.layout.main);
   chatbox = (EditText) findViewById(R.id.Chatbox);
   ...
   chatbox.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
         String data=chatbox.getText().toString();
         ...
      }
   });
}