1

I have a problem with my android project. I am sending data using Datamap to my wear. I have a datalistener for getting data in the wear main activity.

Basically I want to change fragment's textView's text from activity. When I am trying to do this it fails and I got -->NullPointerException. How can I fix that?

Is that because I am calling the fragment in mainactivity`s onCreate method?

MainActivity xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</android.support.v4.view.ViewPager>

Mainactivity

public class MainActivity extends FragmentActivity {

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

        viewpager = (ViewPager) findViewById(R.id.pager);
        FragmentManager fm = getSupportFragmentManager();
        viewpager.setAdapter(new FragmentAdapter(fm));

        dataListener = new DataApi. DataListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onDataChanged(DataEventBuffer dataEvents) {
                final ArrayList<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
                DataMap mediaMap;
                DataMap updateMap;
                for (DataEvent event : events) {
                    PutDataMapRequest putDataMapRequest =
                            PutDataMapRequest.createFromDataMapItem(DataMapItem.fromDataItem(event.getDataItem()));

                    if (event.getType() == DataEvent.TYPE_CHANGED) {
                        if (event.getDataItem().getUri().getPath().equals("/myPath")) {
                            dataMap= DataMapItem.fromDataItem(event.getDataItem()).getDataMap();

                            FragmentA fragment = (MainControlScreenFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentA);

}
}
p3y4m
  • 235
  • 1
  • 11
  • 1
    Please post your whole `MainActivity.class` and it's xml file. – Ivan Ivanov Mar 16 '16 at 12:27
  • 2
    If I understand correct you want to call method from your `MainActivity` to `MainControlScreenFragment` to set some text in the fragment. Am i right? – Ivan Ivanov Mar 16 '16 at 12:48
  • 1
    Yeah, you are rigth! – p3y4m Mar 16 '16 at 12:49
  • I have similar question regarding to this topic, hope someone will help. – chazefate Mar 16 '16 at 14:05
  • If the purpose of the `DataListener` is to update text in the `Fragment`, why not put it in the `Fragment` itself, rather than in the `Activity`? It seems like it would be a more logical structure generally, and would avoid any need to call `findFragmentById` - which seems to be where your problem is (you don't actually specify where the NPE is being thrown). – Sterling Mar 16 '16 at 15:15

1 Answers1

1

Make single ton implementation in your fragment, add this lines in MainControlScreenFragment.class above onCreateView() method:

private static MainControlScreenFragment instance;

public MainControlScreenFragment() {
}

/**
 * Static method which return the instance of the fragment. If the fragment is null, load it,
 * else create new fragment. Singleton pattern.
 *
 * @return
 */
public static MainControlScreenFragment getInstance() {
    if (instance == null) {
        instance = new MainControlScreenFragment();

    }

    return instance;
}

After that in your activity call MainControlScreenFragment.getInstance().changeTextMethod(); instead MainControlScreenFragment fragment = (MainControlScreenFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentA);

Note that your changeTextMethod() should be declared public in MainControlScreenFragment.class

Hope it help!

Ivan Ivanov
  • 902
  • 5
  • 12