私はAndroidから始まり、NavigationViewとTabularレイアウトがあります。私は、ナビゲーションビューでの選択に基づいて、タブフラグメントの値を変更したいと思います。ここでMainActivity.javaタブビューのテキストを更新する
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Context mainActivityContext;
Intent serviceIntent;
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
private SectionsPagerAdapter mSectionsPagerAdapter;
private String valSelected = "";
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editor = sharedPref.edit();
mainActivityContext = this;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.sel1) {
valSelected = "First";
NotificationFragment fragment_obj = (NotificationFragment)getSupportFragmentManager().
findFragmentById(R.id.title_notification);
fragment_obj.updateView();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText("HELLO");
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position == 0) {
return new NotificationFragment();
}
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab1";
case 1:
return "Tab2";
case 2:
return "Tab3";
case 3:
return "Tab4";
}
return null;
}
@Override
public int getItemPosition(Object object) {
if (object instanceof NotificationFragment) {
((NotificationFragment)object).updateView();
}
return super.getItemPosition(object);
}
}
}
私のコード -
ですこれは私のNotificationFragment NotificationFragment.java
public class NotificationFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.notification_layout, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.title_notification);
textView.setText(MainActivity.coinSelected.toUpperCase());
return rootView;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void updateView(){
TextView text = (TextView) this.getView().findViewById(R.id.title_notification);
text.setText(MainActivity.valSelected);
}
}
しかし、私は私のMainActivityの値を更新しようとすると、使用して
NotificationFragment fragment_obj = (NotificationFragment)getSupportFragmentManager().
findFragmentById(R.id.title_notification);
それは私にnull
を与えます。ここを編集
は通知レイアウトファイルです:私のMainActivity.javaに私はandroid.support.v4.app.Fragmentをインポートしたとき、私は一度、この問題に直面した
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/title_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BTC/USD"
android:textSize="26dp"/>
</LinearLayout>
このupdateData()を確認してください。名前use updateView() – MinnuKaAnae
'' R.id.title_notification''このコードの '' findFragmentById(R.id.title_notification); ''はtextViewのようです。フラグメントのコンテナIDでなければなりません。 – Emil
@Emilはい、textViewのIDです。編集を見てください。 – Noober