我從我的一個活動調用片段,並在片段本身中有登錄按鈕。它是給NPE,當我點擊按鈕。我嘗試了所有的排列和組合,使其工作。這裏是我的代碼NPE單擊片段和應用程序崩潰後的按鈕
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUserVisibleHint(true);
View rootView = inflater.inflate(R.layout.fragment1, container, false);
Button button = (Button) rootView.findViewById(R.id.bt_login);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// Perform action on click
EditText userName = (EditText)v.findViewById(R.id.userName);
String user_Name = userName.getText().toString();
EditText password = (EditText)v.findViewById(R.id.password);
String user_Password = password.getText().toString();
if(user_Name.length()== 0 || user_Password.length()== 0)
{
Toast.makeText(getActivity(), "Please enter correct information", Toast.LENGTH_LONG).show();
//Toast.makeText(this,"Please enter correct information",Toast.LENGTH_LONG).show();
return;
}
loginAction(user_Name,user_Password);
}
});
return rootView;
}
堆棧跟蹤
07-11 11:50:02.536: E/AndroidRuntime(429): FATAL EXCEPTION: main
07-11 11:50:02.536: E/AndroidRuntime(429): java.lang.NullPointerException
07-11 11:50:02.536: E/AndroidRuntime(429): at com.androidbegin.sidemenututorial.Fragment1$1.onClick(Fragment1.java:46)
07-11 11:50:02.536: E/AndroidRuntime(429): at android.view.View.performClick(View.java:2408)
07-11 11:50:02.536: E/AndroidRuntime(429): at android.view.View$PerformClick.run(View.java:8816)
07-11 11:50:02.536: E/AndroidRuntime(429): at android.os.Handler.handleCallback(Handler.java:587)
07-11 11:50:02.536: E/AndroidRuntime(429): at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 11:50:02.536: E/AndroidRuntime(429): at android.os.Looper.loop(Looper.java:123)
07-11 11:50:02.536: E/AndroidRuntime(429): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-11 11:50:02.536: E/AndroidRuntime(429): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 11:50:02.536: E/AndroidRuntime(429): at java.lang.reflect.Method.invoke(Method.java:521)
07-11 11:50:02.536: E/AndroidRuntime(429): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-11 11:50:02.536: E/AndroidRuntime(429): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-11 11:50:02.536: E/AndroidRuntime(429): at dalvik.system.NativeStart.main(Native Method)