私はソフトウェア開発とJava全体に不慣れです。ページビューアーを使用した固定タブ付きナビゲーション用にアンドロイドが提供するデフォルトのテンプレートを取得するために、何日も試みてきました。何を試しても、最初のフラグメント以外はロードできないようです。エミュレーターの他のすべての onClick タブ イベントは、テンプレートで定義されたまったく同じ最初のフラグメントをロードし、ページャーをスライドさせても同じ効果があります。
実行時エラーがほとんどなくなるまでコードを何度か変更しようとしましたが、タブを介して他のフラグメントをロードすることはできません。
これについて何か助けていただければ幸いです。
これが私のコードです。
私のプロジェクトはネイティブ タブレット EHR アプリです
package com.example.medictouch;
import java.util.Locale;
import com.example.medictouch.MedicTouchActivity.encountersFragment;
import com.example.medictouch.MedicTouchActivity.patient_chartFragment;
import com.example.medictouch.MedicTouchActivity.billingFragment;
import com.example.medictouch.MedicTouchActivity.medicationsFragment;
import com.example.medictouch.MedicTouchActivity.treatmentsFragment;
import com.example.medictouch.MedicTouchActivity.laboratoryFragment;
import com.example.medictouch.MedicTouchActivity.imagingFragment;
import com.example.medictouch.MedicTouchActivity.doctors_notesFragment;
import com.example.medictouch.MedicTouchActivity.departmentsFragment;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MedicTouchActivity extends FragmentActivity implements
ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medictouch);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new encountersFragment();
Fragment fragment1 = new patient_chartFragment();
Fragment fragment2 = new billingFragment();
Fragment fragment3 = new medicationsFragment();
Fragment fragment4 = new treatmentsFragment();
Fragment fragment5 = new laboratoryFragment();
Fragment fragment6 = new imagingFragment();
Fragment fragment7 = new doctors_notesFragment();
Fragment fragment8 = new departmentsFragment();
return fragment;
}
@Override
public int getCount() {
// Show 6 total pages.
return 10;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toLowerCase();
case 1:
return getString(R.string.title_section2).toLowerCase();
case 2:
return getString(R.string.title_section3).toLowerCase();
case 3:
return getString(R.string.title_section4).toLowerCase();
case 4:
return getString(R.string.title_section5).toLowerCase();
case 5:
return getString(R.string.title_section6).toLowerCase();
case 6:
return getString(R.string.title_section7).toLowerCase();
case 7:
return getString(R.string.title_section8).toLowerCase();
case 8:
return getString(R.string.title_section9).toLowerCase();
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public class encountersFragment extends Fragment {
private final int title_section1 = 0;
public final int ARG_SECTION_NUMBER = title_section1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.encounters,
container, false);
return rootView;
}
}
public class patient_chartFragment extends Fragment {
private final int title_section2 = 1;
public final int ARG_SECTION_NUMBER = title_section2;
public patient_chartFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.patient_chart,
container, false);
return rootView;
}
}
public class billingFragment extends Fragment {
private final int title_section3 = 2;
public final int ARG_SECTION_NUMBER = title_section3;
public billingFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.billing,
container, false);
return rootView;
}
}
public class medicationsFragment extends Fragment {
public final int ARG_SECTION_NUMBER = 3;
public medicationsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.medications,
container, false);
return rootView;
}
}
public class treatmentsFragment extends Fragment {
public final int ARG_SECTION_NUMBER = 4;
public treatmentsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.treatments,
container, false);
return rootView;
}
}
public class laboratoryFragment extends Fragment {
public final int ARG_SECTION_NUMBER = 5;
public laboratoryFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.labs,
container, false);
return rootView;
}
}
public class imagingFragment extends Fragment {
public final int ARG_SECTION_NUMBER = 6;
public imagingFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.imaging,
container, false);
return rootView;
}
}
public class doctors_notesFragment extends Fragment {
public final int ARG_SECTION_NUMBER = 7;
public doctors_notesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.doctors_notes,
container, false);
return rootView;
}
}
public class departmentsFragment extends Fragment {
public final int ARG_SECTION_NUMBER = 8;
public departmentsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.departments,
container, false);
return rootView;
}
}
}