片段内的按钮不起作用

时间:2013-05-16 13:36:22

标签: android android-fragments

我是Android的新手,这里和mi英语很好,我求你理解。

我使用可滚动的滑动+标签创建了一个项目,并根据我的需要对其进行了稍微修改,但是当我尝试在DummySectionFrag中使用按钮时出现错误,试图以多种方式解决它,但我总是得到另一个错误,这里是我的代码,谢谢你的时间。

public class Concejos extends FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter;
final Context context = this;
private static Button button;

ViewPager mViewPager;

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

    // 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);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.concejos, menu);
    return true;
}

/**
 * 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) {

        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);

        switch (position) {
        case 0:
            fragment =new DummySectionFragment();
            break;
        case 1:
            fragment =new DummySectionFragment2();
            break;
        case 2:
            fragment =new DummySectionFragment3();
            break;
        case 3:
            fragment =new DummySectionFragment4();
            break;
        default:
            break;
         }

        return fragment;
    }

    @Override
    public int getCount() {
        // Show 4 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        case 3:
            return getString(R.string.title_section4).toUpperCase(l);
        }
        return null;
    }
}

public static class DummySectionFragment extends Fragment {

    public DummySectionFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_concejos_dummy, container, false);
        button = (Button) rootView.findViewById(R.id.btnDialog);

        // add button listener
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // custom dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.fragment_dialog);
                dialog.setTitle("Title here...");

                // set the custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.txtTexto);
                text.setText("Any text here");
                ImageView image = (ImageView) dialog.findViewById(R.id.ivImagen);
                image.setImageResource(R.drawable.image);

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }

        });
        return rootView;
    }
}


public static class DummySectionFragment2 extends Fragment {

    public DummySectionFragment2() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_concejos_dummy2, container, false);
        return rootView;
    }
}

public static class DummySectionFragment3 extends Fragment {

    public DummySectionFragment3() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_concejos_dummy3, container, false);
        return rootView;
    }
}

public static class DummySectionFragment4 extends Fragment {

    public DummySectionFragment4() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_concejos_dummy4, container, false);
        return rootView;
    }
}

}

0 个答案:

没有答案