0

我是 Android 新手(不是编程,甚至不是 Java),所以请多多包涵。

我正在尝试处理片段的使用。

我有一个使用默认滑动/操作栏创建的项目。我已经进一步扩展它以处理我想要的设置....但是我不太明白发生了什么/如何解决这个问题。

/**
 * 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 DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

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

    @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);
        case 4:
            return getString(R.string.title_section5).toUpperCase(l);
        case 5:
            return getString(R.string.title_section6).toUpperCase(l);
        case 6:
            return getString(R.string.title_section7).toUpperCase(l);
        case 7:
            return getString(R.string.title_section8).toUpperCase(l);
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int position;

        position = getArguments().getInt(ARG_SECTION_NUMBER)-1;
        View rootView;
        TextView dummyTextView;

我真的不想要任何静态或最终的东西,我已经把它大部分解决了,但我不明白下面的行或如何修复它。我有点明白它在做什么。

args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);

错误是:无法对非静态字段 DummySectionFragment.ARG_SECTION_NUMBER 进行静态引用

可能有一个简单的解决方法,我只是对 Android 和 Java 不够熟悉,因为我目前的工作是把所有时间都花在 SQL Server 上。

- 编辑添加我不反对任何静态或最终等。我不太了解的问题是当我想在每个片段中做一些事情时。我在每个布局上都有一个文本视图,我希望能够在循环中操纵它们。我想我被困在一个圈子里,无法找到出路……哈哈。

例如下面我放在上面的代码是

    case 4:
            rootView = inflater.inflate(R.layout.fragment_main_location,container, false);
            dummyTextView= (TextView) rootView .findViewById(R.id.section_label);

            // location
            Button btnShowLocation = (Button) rootView.findViewById(R.id.btnShowLocation);
            Button btnShowDBLocList = (Button) rootView.findViewById(R.id.btnShowDBLocList);
            Button btnLocationsCount = (Button) rootView.findViewById(R.id.btnLocationsCount);
            Button btnTruncateDBLocationsTable = (Button) rootView.findViewById(R.id.btnTruncateDBLocationsTable);

            btnTruncateDBLocationsTable.setOnClickListener(new OnClickListener() {
                @Override                
                public void onClick(View v) {
                    Activity activity = getActivity();
                    int intCount = 0;

                    /*if (activity != null) {
                        //dummyTextView.setText("");
                     try {
                         locationDatabaseHandler.truncateLocationTable();
                         intCount = locationDatabaseHandler.getLocationCount();
                     } catch (Exception e){
                         //dummyTextView.append(e.toString());
                     }
                     //dummyTextView.append("Count:" + intCount + "\n\n");
                     Toast.makeText(activity, "toast_you_just_clicked_a_fragment btnTruncateDBLocationsTable button", Toast.LENGTH_LONG).show();
                    }*/
                }
            });

            dummyTextView = (TextView) rootView    .findViewById(R.id.section_label);
            dummyTextView.append("\nLocation Stuff\n");
            break;

//dummyTextView.append("Count:" + intCount + "\n\n");

我遇到了一个圆圈,如果我 dummyTextView 尝试在 onClick 事件中使用 dummyText,它说我需要将其设为静态(快速修复),并抱怨错误:不能引用非最终变量 dummy7Text在不同方法中定义的 indder 类。

我已经在填充的 onCreate 中添加了一个变量来处理这个问题(LayoutInflater 和 Viewgroup,然后在 onclick 中引用它们(未显示),但是当我进入并实例化时...... textviews 没有任何反应...

有些事情我还没有完全做到,一旦我通过了那个障碍,我就会全力以赴,并且能够让它做我想做的事情。

4

2 回答 2

1

我真的不想要任何静态或最终的东西

为什么?它们不会对性能产生负面影响,也不是编码实践不佳的标志。

我不明白以下行

每个 Fragment 都可以使用包含任意数量的键值对的 Bundle 创建。DummySectionFragment.ARG_SECTION_NUMBER是键(字符串),position + 1是值。因此,这段代码告诉新DummySectionFragment的片段应该显示哪一部分内容。

此方法比将这些参数放在构造函数中更可取,因为不能保证调用 Fragment 的自定义构造函数。Android 生成 Fragments 的方式有很多,所以这降低了出现 NullPointerExceptions 等问题的可能性。

错误是:无法对非静态字段 DummySectionFragment.ARG_SECTION_NUMBER 进行静态引用

如您所知,DummySectionFragment.ARG_SECTION_NUMBER它指的是DummySectionFragment类中的一个静态字段,称为ARG_SECTION_NUMBER. 通过使该字段非静态,您不能再在没有DummySectionFragment实例的情况下引用该常量值。

另一种选择(如果您真的不想要静态字段)是对字符串进行硬编码。因此,您的代码将是:

args.putInt("section_number", position + 1);

但是,公共静态字段是一种更好的编码实践,可以防止字符串中的拼写错误。

我遇到了一个圈子,如果我 dummyTextView 尝试在 onClick 事件中使用 dummyText,它说我需要将其设为静态(快速修复)并抱怨错误:不能引用非最终变量 dummy7Text 里面在不同方法中定义的 indder 类。

我不会使用匿名内部类,而是让您的 Fragment 实现OnClickListener.

例如:

public class MyFragment extends Fragment implements OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // ...

         Button btnTruncateDBLocationsTable = (Button) rootView.findViewById(R.id.btnTruncateDBLocationsTable);
         btnTruncateDBLocationsTable.setOnClickListener(this);

        // ...
    }

    @Override
    public void onClick(View v) {
        // You can reference dummyTextView here without any problems
    }
}
于 2013-10-29T20:36:40.097 回答
0

这意味着ARG_SECTION_NUMBER应该声明为public static. 如果它声明为更好 public static final

于 2013-10-29T20:34:52.150 回答