【问题标题】:User view in fragment for methods方法片段中的用户视图
【发布时间】:2017-09-08 05:03:46
【问题描述】:

所以我有一个片段,在它的 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 时,它会作为空引用返回……知道为什么吗? 谢谢您的帮助! 发现的问题: 我在 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 里面...知道如何让它发送正确的视图吗?

【问题讨论】:

  • 是的,我检查了很多次
  • 对不起,误删了我原来的评论。您可以从dashboard_fragment 访问任何其他控件以查看会发生什么吗?
  • dashboar_fragment 没有返回任何内容..没有元素
  • 你有多个dashboard_fragment (不同分辨率)?我在您的代码上没有看到任何奇怪的东西,它应该可以工作。我还会提供一个干净的构建,以防它卡在您的旧版布局上。

标签: android android-fragments


【解决方案1】:

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

【讨论】:

  • 它是它的一部分
  • 你能发布xml吗?
  • 我已经贴出来了
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    onViewCreated 中,视图已经膨胀,所以它是可用的,发生在onCreateView 之后,在方法的参数中,您可以找到View,它是膨胀的视图,使用该视图您可以找到里面的任何东西。

    @Override
    onViewCreated(... View view) {
    
        ScrollView dashboardData = (ScrollView) view.findViewById(R.id.dashboardScrollView);
    
    }
    

    【讨论】:

      【解决方案4】:

      问题出在我没有发布的东西上,一个微调器的 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
                  }
              });
      

      谢谢大家,很抱歉没有发布所有内容!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        • 1970-01-01
        相关资源
        最近更新 更多