方法片段中的用户视图

时间:2017-09-07 20:34:03

标签: android android-fragments

所以我有一个片段,在其onCreateView方法中我调用其他方法,我尝试将视图作为参数发送:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dashboard_fragment, container, false);

        getDashboardDBData(view, periodKey, periodValue);

        return view;
    }

//Get data for dashboard from Database
    private void getDashboardDBData(final View view, final String periodKey, final String periodValue){

        final ScrollView dashboardData = (ScrollView) view.findViewById(R.id.dashboardScrollView);
    }

XML(它的一部分 - 更精确的开头):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@android:style/TextAppearance.DeviceDefault.Medium"
    xmlns:autofit="http://schemas.android.com/apk/res-auto">
    <ProgressBar
        android:id="@+id/dashboardUpperLoading"
        style="?android:attr/progressBarStyle"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="65dp"
        android:elevation="17dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.007"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1"
        tools:layout_editor_absoluteX="163dp" />

    <ProgressBar
        android:id="@+id/dashboardMainLoading"
        style="?android:attr/progressBarStyle"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="142dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/dashboardScrollView"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:text="Period"
        app:layout_constraintBottom_toTopOf="@+id/periodSpinner"
        app:layout_constraintLeft_toLeftOf="@+id/periodSpinner"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1" />

    <Spinner
        android:id="@+id/periodSpinner"
        android:layout_width="112dp"
        android:layout_height="31dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        app:layout_constraintBottom_toTopOf="@+id/dashboardScrollView"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_editor_absoluteX="216dp" />

    <ScrollView
        android:id="@+id/dashboardScrollView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1">

问题是在getDashboardDBData中,当我尝试访问dashboardScrollView时,它返回为null引用...任何想法为什么? 谢谢您的帮助! 问题被发现: 我在onCreateView里面有一个带有onClickListener的微调器(不认为它是相关的)它调用了getDashboardDBData,但是它发送的视图是不同的,看:

dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String periodKey= dropdown.getSelectedItem().toString();
                String periodValue = lPeriods.get(periodKey);

                setPeriodKey(periodKey);
                setPeriodValue(periodValue);

                dropdown.getSelectedView();
                dropdown.setEnabled(false);

                getDashboardDBData(view, periodKey, periodValue);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

                // sometimes you need nothing here
            }
        });

这个监听器在onViewCreated里面......任何想法如何让它发送正确的视图?

4 个答案:

答案 0 :(得分:0)

这意味着在充气视图中不存在ID为R.id.dashboardScrollView的视图。确保它是视图层次结构的一部分。

答案 1 :(得分:0)

这是因为尚未创建视图并且您已将视图传递给方法getDashboardDBData并在该视图上调用findViewById。我建议使用onViewCreated表示视图已创建该片段,您现在可以找到子视图

答案 2 :(得分:0)

这可能会发生,因为onCreateView创建了Fragment的视图,但这并不意味着可用。还有另一种方法onViewCreatedonCreateView之后立即调用。

onViewCreated中,视图已经膨胀,因此它可用,在onCreateView之后发生,并且在方法的参数中,您可以找到View,它是视图膨胀,使用该视图,您可以在其中找到任何内容。

@Override
onViewCreated(... View view) {

    ScrollView dashboardData = (ScrollView) view.findViewById(R.id.dashboardScrollView);

}

答案 3 :(得分:0)

问题在于我没有发布的东西,一个微调器的onItemSelectedListener:

dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String periodKey= dropdown.getSelectedItem().toString();
                String periodValue = lPeriods.get(periodKey);

                setPeriodKey(periodKey);
                setPeriodValue(periodValue);

                dropdown.getSelectedView();
                dropdown.setEnabled(false);

                getDashboardDBData(view, periodKey, periodValue);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

                // sometimes you need nothing here
            }
        });

监听器有自己的视图,所以我只是改变了参数的名称:

dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
                String periodKey= dropdown.getSelectedItem().toString();
                String periodValue = lPeriods.get(periodKey);

                setPeriodKey(periodKey);
                setPeriodValue(periodValue);

                dropdown.getSelectedView();
                dropdown.setEnabled(false);

                getDashboardDBData(view, periodKey, periodValue);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

                // sometimes you need nothing here
            }
        });

谢谢大家,我很抱歉没有张贴所有内容!