【问题标题】:Android Fragment issueAndroid 片段问题
【发布时间】:2013-10-29 20:32:13
【问题描述】:

我是 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,它说我需要将其设为静态(快速修复)并抱怨错误:不能引用非最终在不同方法中定义的 indder 类中的变量 dummy7Text。

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

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

【问题讨论】:

    标签: java android static android-fragments


    【解决方案1】:

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

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

    下面这行我没看懂

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

    这种方法比将这些参数放在构造函数中更可取,因为不能保证调用 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,它说我需要将其设置为静态(快速修复)并抱怨错误:不能引用非最终在不同方法中定义的 indder 类中的变量 dummy7Text。

    我会让你的 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
        }
    }
    

    【讨论】:

    • 好的,我开始对正在发生的事情有所了解,请参阅我的编辑和补充以进一步澄清我想要做什么。我想我很接近了。
    • 关于OnClickListener的其他问题-在这种情况下,我建议让您的片段实现OnClickListener,而不是尝试使用匿名内部类。这样您就不必担心将变量设置为静态或最终变量,以便它们可用于内部类。
    • 例如,会有很大帮助吗?我以前用 Java 写过,但可能已经有 8 年了......我正在挖掘自己的方式,我花更多的时间来管理数据库(DBA)而不是编码,但是我喜欢编码.. . 我在每个片段上都有放置按钮,以允许我在另一个项目中实现的各种功能,并且我试图通过新界​​面复制功能(这就是我什至在做这个 atm 的原因)。
    • 谢谢。试图确保我理解正确。如果我添加它,我应该能够在按钮的 onclick 事件中访问我的 Textview 吗?我不得不说我越来越喜欢这个网站,因为我收到了高质量的答案。感谢您的帮助!如果我的理解是正确的,那么我会将此标记为答案,并且应该超过 15 个代表并标记您。
    • 是的,在我的示例中,您可以在 onClick 函数中访问您的 TextView。请务必致电btnTruncateDBLocationsTable.setOnClickListener(this)
    【解决方案2】:

    这意味着ARG_SECTION_NUMBER 应该被声明为public static。最好声明为public static final

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多