-1

I tried to write app using ViewPager and I want to add a listener to TextView in my app. Adding listener in Fragment code is simple, but how can I create it in MainActivity without getting NullPointerException ?

MainActivity:

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener{
ViewPager viewPager;
TabHost tabHost;


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

    initTabHost();
    initViewPager();




    final TextView textView = (TextView) findViewById(R.id.txt_frag1);

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setText("Changed text");
        }
    });

}




private void initTabHost() {
    tabHost = (TabHost) findViewById(R.id.tabHost);
    tabHost.setup();

    String[] tabNames = {"FRAG1","FRAG2"};

    for(int i=0; i<tabNames.length;i++){
        TabHost.TabSpec tabSpec;
        tabSpec = tabHost.newTabSpec(tabNames[i]);
        tabSpec.setIndicator(tabNames[i]);
        tabSpec.setContent(new FakeContent(getApplicationContext()));
        tabHost.addTab(tabSpec);
    }
    tabHost.setOnTabChangedListener(this);
}

public class FakeContent implements TabHost.TabContentFactory{

    Context context;
    public FakeContent(Context mcontext){
        context=mcontext;
    }

    @Override
    public View createTabContent(String tag) {
        View fakeView = new View(context);
        fakeView.setMinimumHeight(0);
        fakeView.setMinimumWidth(0);
        return fakeView;
    }
}

private void initViewPager() {

    viewPager = (ViewPager) findViewById(R.id.view_pager);
    List<Fragment> listFragments = new ArrayList<Fragment>();
    listFragments.add(new BlankFragment());
    listFragments.add(new BlankFragment2());


    MyFragmentAdapter myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(),listFragments);

    viewPager.setAdapter(myFragmentAdapter);
    viewPager.setOnPageChangeListener(this);
}


@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {

}

@Override
public void onPageScrollStateChanged(int selectedItem) {
    tabHost.setCurrentTab(selectedItem);
}

@Override
public void onTabChanged(String tabId) {
    int selectedItem = tabHost.getCurrentTab();
    viewPager.setCurrentItem(selectedItem);
    //TabHost ustawianie na środku
    HorizontalScrollView hScrollView = (HorizontalScrollView) findViewById(R.id.h_scroll_view);
    View tabView = tabHost.getCurrentTabView();
    int scrollPos = tabView.getLeft() -(hScrollView.getWidth() -tabView.getWidth())/2;
    hScrollView.smoothScrollTo(scrollPos,0);
}
}

MyFragmentAdapter:

public class MyFragmentAdapter extends FragmentPagerAdapter {

List<Fragment> listFragments;

public MyFragmentAdapter(FragmentManager fm, List<android.support.v4.app.Fragment> listFragments) {
    super(fm);
    this.listFragments = listFragments;
}

@Override
public android.support.v4.app.Fragment getItem(int position) {
    return listFragments.get(position);
}

@Override
public int getCount() {
    return listFragments.size();
}


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_smart_led"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pietr.viewpagerr.MainActivity">



<TabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/h_scroll_view"
            android:fillViewport="true"
            android:scrollbars="none">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></TabWidget>
        </HorizontalScrollView>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.view.ViewPager
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/view_pager"
                ></android.support.v4.view.ViewPager>

        </FrameLayout>
    </LinearLayout>
</TabHost>

</LinearLayout>

Fragments are standard blank fragments with one TextView.

pietr
  • 1
  • 1

2 Answers2

0

There is no view by the id txt_frag1 in you activity_main layout.Because of this you are getting a NPE.First add a textview with the same in activity_main xml and then access it in java.

Ajit Pratap Singh
  • 1,299
  • 12
  • 24
0

I have a TextView by the id txt_frag1 defined in fragment_blank.xml . I want to display this fragment in Pager and get access to it from MainActivity.

pietr
  • 1
  • 1