在片段

时间:2017-12-29 07:31:52

标签: android

我试图在活动开始时设置EditText值,这是我正在做的事情:

      @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            SharedPreferences mPrefs = this.getActivity().getSharedPreferences("userinfoafterlogin", MODE_PRIVATE); //add key
            String response = mPrefs.getString("userinfo", null);

            try{
                JSONObject json = new JSONObject(response);
                json = (JSONObject)json.get("nameValuePairs");
                json = (JSONObject)json.get("userData");
                json = (JSONObject)json.get("nameValuePairs");
                String fullname = (String)json.get("fullname");
                String username = (String)json.get("username");

                EditText editText = (EditText)getActivity().findViewById(R.id.fullnametext);
                editText.setText(fullname);

                Log.i("data", " retrieve --> " + fullname);
            }catch (JSONException e) {
                Log.e("MYAPP", "unexpected JSON exception", e);
            }
  }

但是,当我运行它时,我收到此错误

  

12-29 14:29:48.321 3273-3273 / com.example.boby.srsly E / AndroidRuntime:   致命异议:主要                                                                         处理:com.example.boby.srsly,PID:3273                                                                         显示java.lang.NullPointerException                                                                             在   com.example.boby.srsly.ProfilFragment.onCreate(ProfilFragment.java:71)                                                                             在android.app.Fragment.performCreate(Fragment.java:1678)                                                                             在   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)                                                                             在   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)                                                                             在android.app.BackStackRecord.run(BackStackRecord.java:684)                                                                             在   android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)                                                                             在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:443)                                                                             在android.os.Handler.handleCallback(Handler.java:733)                                                                             在android.os.Handler.dispatchMessage(Handler.java:95)                                                                             在android.os.Looper.loop(Looper.java:136)                                                                             在android.app.ActivityThread.main(ActivityThread.java:5019)                                                                             at java.lang.reflect.Method.invokeNative(Native Method)                                                                             在java.lang.reflect.Method.invoke(Method.java:515)                                                                             在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                                                                             在dalvik.system.NativeStart.main(本地方法)

我该如何解决?

我在onCreateView

中移动了它
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_profil, container, false);
    SharedPreferences mPrefs = this.getActivity().getSharedPreferences("userinfoafterlogin", MODE_PRIVATE); //add key
    String response = mPrefs.getString("userinfo", null);

    try{
        JSONObject json = new JSONObject(response);
        json = (JSONObject)json.get("nameValuePairs");
        json = (JSONObject)json.get("userData");
        json = (JSONObject)json.get("nameValuePairs");
        String fullname = (String)json.get("fullname");
        String username = (String)json.get("username");

        EditText editText = (EditText)v.findViewById(R.id.fullnametext);
        editText.setText(fullname);
        Log.i("data", " retrieve --> " + fullname);
        return v;
    }catch (JSONException e) {
        Log.e("MYAPP", "unexpected JSON exception", e);
        return v;
    }

}

现在我收到了这个错误

  

12-29 14:35:09.801 3383-3383 / com.example.boby.srsly E / AndroidRuntime:   致命异议:主要                                                                         处理:com.example.boby.srsly,PID:3383                                                                         java.lang.ClassCastException:   android.support.v7.widget.AppCompatTextView无法强制转换为   android.widget.EditText                                                                             在   com.example.boby.srsly.ProfilFragment.onCreateView(ProfilFragment.java:78)                                                                             在android.app.Fragment.performCreateView(Fragment.java:1700)                                                                             在   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)                                                                             在   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)                                                                             在android.app.BackStackRecord.run(BackStackRecord.java:684)                                                                             在   android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)                                                                             在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:443)                                                                             在android.os.Handler.handleCallback(Handler.java:733)                                                                             在android.os.Handler.dispatchMessage(Handler.java:95)                                                                             在android.os.Looper.loop(Looper.java:136)                                                                             在android.app.ActivityThread.main(ActivityThread.java:5019)                                                                             at java.lang.reflect.Method.invokeNative(Native Method)                                                                             在java.lang.reflect.Method.invoke(Method.java:515)                                                                             在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                                                                             在dalvik.system.NativeStart.main(本地方法)

2 个答案:

答案 0 :(得分:0)

首先,您需要覆盖onActivityCreated()中的fragment方法,并在EditText方法内初始化activitycreated()

EditText editText ;
像这样

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    editText = (EditText)getActivity().findViewById(R.id.fullnametext);
            editText.setText(fullname);
}

答案 1 :(得分:0)

首先,您需要了解fragment生命周期Fragments。在onCreate of fragment中,不会创建视图。视图在onCreateView片段方法

中创建
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View v = inflater.inflate(R.layout.yourView, container, false);
 editText = (EditText)v.findViewById(R.id.fullnametext);
        editText.setText(fullname);
}

因此,您需要在此方法中移动布局小部件。

关于你的第二个错误,你可能会投出错误的小部件:

  

EXCEPTION:main进程:com.example.boby.srsly,PID:3383 java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView无法强制转换为android.widget.EditText

所以你需要这样做:

AppCompatTextView textview;
textview= (AppCompatTextView )v.findViewById(R.id.fullnametext);
            textview.setText(fullname);

或者如果它是您想要的编辑文本,则需要将AppCompatTextView更改为EditText