简体   繁体   中英

How to initialize a EditText when the Fragment's method is called from activity?

i work on a NotePad application, because i wanted to try SQLite and i don't have a correct NotePad app on my smartphone.

Screenshot of the app

So my app is composed of a Activity which contains a Viewpager and a menu for save the Note, create a new Note or delete the current note, and a Fragment where is a simple EditText.

I create a custom object Note which is compose of a String title, a String content and a Integer id for save the data on SQLite.

So my problem is for the functionnality of saving the Note on my database.

Its start with the menu in the Action Bar of the Activity, so when i click on the button for Saving (the second which look like a SD card), i called a method from my Fragment for edit the Note on the database like this :

private void save() {
    TabFragment fragment = new TabFragment();
    fragment.editNote(arrayListTitle);
}

The arraylist contains the title of every note (i use him for create the TabLayout) and i pass him for get the title of the note.

So there is the code of my Fragment :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootview = (ViewGroup) inflater.inflate(R.layout.fragment_tab, container, false);
    editText1 = rootview.findViewById(R.id.editText1);

    manager = new NoteManager(getActivity());
    manager.open();

    Bundle args = getArguments();
    position = args.getInt("0");
    arrayListContent = args.getStringArrayList("1");

    editText1.setText(arrayListContent.get(position));
    return rootview;
}

public void editNote(ArrayList<String> arrayListTitle) {
    title_edit = arrayListTitle.get(position);
    content_edit = editText1.getText().toString();
    Note note = manager.getNote(position + 1);

    note.setTitle(title_edit);
    note.setContent(content_edit);
    manager.modNote(note);
}

When i go in the editNote method i receive the error :

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: fr.pensebete, PID: 24165
              java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at fr.pensebete.TabFragment.editNote(TabFragment.java:50)
                  at fr.pensebete.MainActivity.save(MainActivity.java:130)
                  at fr.pensebete.MainActivity.onOptionsItemSelected(MainActivity.java:101)
                  at android.app.Activity.onMenuItemSelected(Activity.java:3369)
                  at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:368)
                  at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
                  at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
                  at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
                  at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:65)
                  at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:202)
                  at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780)
                  at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                  at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
                  at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
                  at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963)
                  at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
                  at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150)
                  at android.view.View.performClick(View.java:6213)
                  at android.widget.TextView.performClick(TextView.java:11074)
                  at android.view.View$PerformClick.run(View.java:23645)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6692)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

I know its because the EditText is not initialize because i called the editNote() method only and the OnCreateView is not called.

How can i call the EditNote() method from the Activity and make the EditText and the variable position initialize correctly (like in the OnCreateView method)? Can you help me because i search since 3 hours without a solution to make it works.

Thanks for your time and your help, and sorry for my bad english.

Edit : During i write this question i realize its maybe because i do a new TabFragment in my Activity's method :

private void save() {
    TabFragment fragment = new TabFragment();
    fragment.editNote(arrayListTitle);
}

Maybe i must receive or find the current fragment already created, but i don't know how to do that. HELP !

Try this:

private void save() {
    TabFragment fragment = getFragmentManager().findFragmentByTag("Tag");
    fragment.editNote(arrayListTitle);
}

Be sure that when you create your fragment t you set the tag like so:

getFragmentManager().beginTransaction().replace(R.id.frame, new TabFragment(), "Tag").commit();

For example refer this

https://github.com/codepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter

In Activity Class

public class MainActivity extends AppCompatActivity {

Adapter adapterViewPager; //Replace with your adapter
ViewPager vpPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    vpPager = (ViewPager) findViewById(R.id.vpPager);
    adapterViewPager = new ViewPagerAdapter(getSupportFragmentManager());
    vpPager.setAdapter(adapterViewPager);
}

private void save() {
  TabFragment fragment = vPager.getItem(0) //position to which fragment
  fragment.editNote(arrayListTitle);
}
}

Hope it helps.!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM