我有以下代码。
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)