设置EditText.setText()会导致应用程序崩溃

时间:2014-07-13 03:21:00

标签: java android

我是一名学习android的iOS开发人员,我遇到了这个问题,无法在任何地方找到它。这就是我的代码

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

这让我疯了,任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:1)

问题在于检查站5之后:

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

不是从Activity创建EditText,而是使用在片段中膨胀的rootView:

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