2014-07-13 42 views
0

我是一名iOS開發者學習Android和正在運行到這個問題,並不能找到它anywhere.Here是我的代碼設置一個EditText.setText()崩潰的應用程序

public static EditText adminUsername; 
public static String restoredUsername; 



public static class PlaceholderFragment extends Fragment { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_admin, 
       container, false); 

     Log.d("checkpoint", "1"); 
     //if there is a previous successful login, autofill username 
     Log.d("checkpoint", "2"); 
     SharedPreferences prefs = getActivity().getPreferences(MODE_PRIVATE); 
     Log.d("checkpoint", "3"); 
     restoredUsername = prefs.getString("adminusername",""); 
     if(!restoredUsername.equals("")) 
      { 
      //if it isn't blank, restore it 

       Log.d("checkpoint", "5"); 
       adminUsername = (EditText)getActivity().findViewById(R.id.adminusername); 
       Log.d("checkpoint", "6"); 
       Log.d("username", restoredUsername); 

       adminUsername.setText(restoredUsername);  
       Log.d("checkpoint", "7"); 
      } 


     return rootView; 
    } 
} 

檢查點6和訴訟後, log.d,應用程序在adminUsername.setText(restoredUsername)上崩潰。

這裏是日誌

07-12 23:10:11.424: D/checkpoint(5105): 1 
07-12 23:10:11.424: D/checkpoint(5105): 2 
07-12 23:10:11.434: D/checkpoint(5105): 3 
07-12 23:10:11.444: D/checkpoint(5105): 5 
07-12 23:10:11.444: D/checkpoint(5105): 6 
07-12 23:10:11.444: D/username(5105): alexatwater 
07-12 23:10:11.444: D/AndroidRuntime(5105): Shutting down VM 
07-12 23:10:11.444: W/dalvikvm(5105): threadid=1: thread exiting with uncaught exception (group=0xb0d2ab20) 
07-12 23:10:11.464: E/AndroidRuntime(5105): FATAL EXCEPTION: main 
07-12 23:10:11.464: E/AndroidRuntime(5105): Process: com.alexatwater.elhsapcalc, PID: 5105 
07-12 23:10:11.464: E/AndroidRuntime(5105): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alexatwater.elhsapcalc/com.alexatwater.elhsapcalc.AdminActivity}: java.lang.NullPointerException 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.os.Handler.dispatchMessage(Handler.java:102) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.os.Looper.loop(Looper.java:136) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at java.lang.reflect.Method.invoke(Method.java:515) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at dalvik.system.NativeStart.main(Native Method) 
07-12 23:10:11.464: E/AndroidRuntime(5105): Caused by: java.lang.NullPointerException 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at com.alexatwater.elhsapcalc.AdminActivity$PlaceholderFragment.onCreateView(AdminActivity.java:186) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.Fragment.performCreateView(Fragment.java:1700) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.BackStackRecord.run(BackStackRecord.java:684) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.Activity.performStart(Activity.java:5240) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168) 
07-12 23:10:11.464: E/AndroidRuntime(5105):  ... 11 more 
07-12 23:10:13.444: I/Process(5105): Sending signal. PID: 5105 SIG: 9 

這是推動我瘋了,任何幫助將是非常讚賞!

+0

請告訴我行186?那就是你有一個空指針異常 – SeahawksRdaBest

回答

1

的問題就在這裏檢查點後5:

adminUsername = (EditText)getActivity().findViewById(R.id.adminusername); 

而不是從活動創建的EditText的,使用rootView在你的片段,你虛增:

adminUsername = (EditText)rootView.findViewById(R.id.adminusername); 
+0

如果這解決了你的問題,請把它作爲正確的。謝謝 –