Android 无法在片段容器中执行活动的方法
我是Android新手,对我难看的英语感到抱歉 我有2个片段,其中的主要任务是: ProfileFragment.javaAndroid 无法在片段容器中执行活动的方法,android,exception,Android,Exception,我是Android新手,对我难看的英语感到抱歉 我有2个片段,其中的主要任务是: ProfileFragment.java private void initUI(View parent) { /** * referencing of fragment_profile */ fragmentImageProfile = (CircleImageView) parent.findViewById(R.id.fragment_profile_picture);
private void initUI(View parent) {
/**
* referencing of fragment_profile
*/
fragmentImageProfile = (CircleImageView) parent.findViewById(R.id.fragment_profile_picture);
fragmentName = (EditText) parent.findViewById(R.id.fragment_profile_editText_name);
fragmentFamily = (EditText) parent.findViewById(R.id.fragment_profile_editText_family);
fragmentPhoneNumber = (EditText) parent.findViewById(R.id.fragment_profile_editText_phone);
/**
* get contact by invoke from CallBack on ProfileActivity
*/
Contact contact = profileCallBack.getContact();
fragmentName.setText(contact.getName());
fragmentFamily.setText(contact.getFamily());
fragmentPhoneNumber.setText(contact.getPhonNumber());
Log.i("==>", "initUI: "+ contact.getName());
}
public void updateContact() {
int id = contact.get_id();
String name = fragmentEditEditTextName.getText().toString();
Log.i("==>", "Name Update: " + name);
String family = fragmentEditEditTextFamily.getText().toString();
String phoneNumber = fragmentEditEditTextPhoneNumber.getText().toString();
int rowUpdate = App.getInstanceImplementation().updateContact(
new Contact(id, name, family, phoneNumber, "image"));
Log.i("==>", "btnUpdateContact: " + rowUpdate);
}
和它的Lauout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: "
android:textSize="20dp" />
<EditText
android:id="@+id/fragment_profile_editText_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:enabled="false"
android:focusable="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family: "
android:textSize="20dp" />
<EditText
android:id="@+id/fragment_profile_editText_family"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:focusable="false"
android:inputType="none" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone: "
android:textSize="20dp" />
<EditText
android:id="@+id/fragment_profile_editText_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:focusable="false"
android:inputType="none" />
</LinearLayout>
这个布局呢
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: "
android:textSize="20dp" />
<EditText
android:id="@+id/fragment_edit_editText_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:inputType="text"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family: "
android:textSize="20dp" />
<EditText
android:id="@+id/fragment_edit_editText_family"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone: "
android:textSize="20dp" />
<EditText
android:id="@+id/fragment_edit_editText_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/fragment_edit_btn_save"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginRight="2dp"
android:onClick="btnUpdateContact"
android:layout_weight="1"
android:background="#8d860000"
android:padding="12dp"
android:text="Update" />
<Button
android:id="@+id/fragment_edit_btn_exit"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:onClick="btnExitContact"
android:layout_weight="1"
android:background="#8d860000"
android:padding="12dp"
android:text="Exit" />
</LinearLayout>
问题在于:
private ProfileFragment getProfileFragment() {
if (profileFragment == null) {
profileFragment = new ProfileFragment(); //you need to change this as this will only create an object and doesn't initialize any view;
}
Log.i("==>", "getProfileFragment:" + profileFragment.getId());
return profileFragment;
}
现在如果调用updateContact()方法,当您试图访问FragmentEditTextName文本视图时,它将在从上述方法接收的对象上引发异常
更改getProfileFragment()中的以下行:
致:
原因:com.example.sayres.myapplication7.mvp.view.profile.EditProfileFragment.updateContact上的java.lang.NullPointerException(EditProfileFragment.java:70)在com.example.sayres.myapplication7.mvp.view.profile.ProfileActivity.btnuupdateContact(ProfileActivity.java:97)中,我认为您需要更改getEditProfileFragment();实施不要创建片段的新实例,而是尝试从为view pagerok创建的列表中获取它,但不能为null,因为它是在此方法中初始化的
private EditProfileFragment getEditProfileFragment(){if(EditProfileFragment==null){EditProfileFragment=new EditProfileFragment();}
我在onResume方法上运行了什么样的更改?@sanjeetkumarsingh它肯定不会为空,但使用new关键字创建新的片段实例并不意味着该片段的所有视图都已创建。在updateCon中EditProfileFragment中的tact()方法您试图访问FragmentEditTextName,这会导致错误。
02-04 01:23:40.953 4545-4545/com.example.sayres.myapplication7 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sayres.myapplication7, PID: 4545
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.sayres.myapplication7.mvp.view.profile.EditProfileFragment.updateContact(EditProfileFragment.java:70)
at com.example.sayres.myapplication7.mvp.view.profile.ProfileActivity.btnUpdateContact(ProfileActivity.java:97)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
private ProfileFragment getProfileFragment() {
if (profileFragment == null) {
profileFragment = new ProfileFragment(); //you need to change this as this will only create an object and doesn't initialize any view;
}
Log.i("==>", "getProfileFragment:" + profileFragment.getId());
return profileFragment;
}
profileFragment = new ProfileFragment();
profileFragment = fragments.get(1);