0

我有以下代码。

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)
4

2 回答 2

2

试试这个:

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

此外,请确保您所指的 EditText 位于您用于通过以下方式映射您的活动的同一个 xml 文件中setContentView()

于 2012-07-15T12:46:55.040 回答
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();
         ...
      }
   });
}
于 2012-07-15T13:04:58.033 回答