2013-07-11 127 views
0

我從我的一個活動調用片段,並在片段本身中有登錄按鈕。它是給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) 

回答

0

您應該使用rootView誇大你的意見。

EditText userName = (EditText)rootView.findViewById(R.id.userName); 
0

v是你點擊,沒有佈局,所以你不應該叫v.findViewById(...)的單一視圖。嘗試

EditText userName = (EditText)findViewById(R.id.userName); 
String user_Name = userName.getText().toString(); 
EditText password = (EditText)findViewById(R.id.password); 
0

您應該使用視圖對象(rootview)來初始化意見

EditText userName,password; // delcare variables as class member. 
View rootView = inflater.inflate(R.layout.fragment1, container, false); 
userName = (EditText)rootview.findViewById(R.id.userName); 
password = (EditText)rootview.findViewById(R.id.password); 

也沒有必要initalize按鈕點擊美景每次。

移動按鈕點擊之外的初始化。

相關問題