无法在片段容器中执行活动的方法

时间:2017-02-04 10:25:49

标签: android exception

我是Android新手,对我丑陋的英语感到抱歉

我有两个片段,主要任务是:

ProfileFragment.java

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());

}

及其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>

在这个片段中,我将联系人的字段设置为3个编辑文本。 EditProfileFragment.java

 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);

}

和这个布局

  <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>

我更新了数据库。 在我的ProfileActivity中,我使用ViewPager通过CallBack Listener显示这些片段和这些片段与活动的连接。

public class ProfileActivity extends FragmentActivity implements ProfileFragment.ProfileCallBack , EditProfileFragment.EditFragmentCallBack {
private ProfileFragment profileFragment;
private EditProfileFragment editProfileFragment;
private PagerAdapterFragment adapterFragment;
private List<Fragment> fragments;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);


}



@Override
protected void onResume() {
    super.onResume();
    getProfileFragment();
    getEditProfileFragment();
    initFragment();
}

private void initFragment() {
    fragments = new Vector<>();
    fragments.add(Fragment.instantiate(this, ProfileFragment.class.getName()));
    fragments.add(Fragment.instantiate(this, EditProfileFragment.class.getName()));

    adapterFragment = new PagerAdapterFragment(this.getSupportFragmentManager(), fragments);
    ViewPager pager = (ViewPager) findViewById(R.id.vpg_main_content);
    pager.setAdapter(adapterFragment);

}

private ProfileFragment getProfileFragment() {
    if (profileFragment == null) {
        profileFragment = new ProfileFragment();
    }
    Log.i("==>", "getProfileFragment:" + profileFragment.getId());
    return profileFragment;
}

private EditProfileFragment getEditProfileFragment() {
    if (editProfileFragment == null) {
        editProfileFragment = new EditProfileFragment();
    }
    Log.i(">", "getEditProfileFragment: "+ editProfileFragment.getId());
    return editProfileFragment;
}


@Override
public Contact getContact() {
    Intent intent = getIntent();
    return (Contact) intent.getSerializableExtra(Contact.class.getName());
}

@Override
public void finishProfile() {
    // TODO: 2/3/2017
//        finish();
}


public void btnUpdateContact(View view) {
    getEditProfileFragment().updateContact();

}

public void btnExitContact(View view) {
//        getEditProfileFragment().btnExitContact();
        finish();
}

但在此行public void btnUpdateContact(View view)中出现此错误:

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) 

1 个答案:

答案 0 :(得分:0)

问题在于:

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;
}

现在,如果您对从上面的方法收到的对象调用 updateCon tact()方法,则在您尝试访问fragmentEditEditTextName文本视图时会抛出异常。

更改getProfileFragment()中的以下行:

profileFragment = new ProfileFragment();

到:

profileFragment = fragments.get(1);