Android,setOnClickLister错误,我看不出有什么问题

时间:2017-12-14 02:28:50

标签: android android-fragments onclicklistener android-scrollview

我一直收到以下错误:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ScrollView.setVisibility(int)' on a null object reference

单击按钮时,我希望scrollView及其内容隐藏。

但我不明白错误。我将我的按钮连接到scrollView但它不起作用。这是片段中按钮的代码:

 public class SavedBroad extends Fragment {

Button activityButtonPhoto;
public ScrollView broadScroll;


Button activityButtonBroad;
public ScrollView photoScroll;

View rootView;


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


     broadScroll = rootView.findViewById(R.id.BroadScrollView); 
   // photoScroll = rootView.findViewById(R.id.PhotoScrollView);  


  //  broadScroll= getActivity().findViewById(R.id.BroadScrollView); 
  // photoScroll=getActivity().findViewById(R.id.PhotoScrollView);




    activityButtonPhoto= rootView.findViewById(R.id.PhotoVid);
    activityButtonPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            broadScroll.setVisibility(View.INVISIBLE);

        }

    });

    return rootView;

    }


   } 

这是我的XML文件:

<Button
    android:id="@+id/PhotoVid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="6dp"
    android:background="@android:color/holo_green_dark"
    android:text="PhotoVid"
    app:layout_constraintHorizontal_bias="0.155"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button

    android:id="@+id/broad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="6dp"
    android:background="@android:color/holo_red_dark"
    android:text="Broadcast"
    app:layout_constraintHorizontal_bias="0.741"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ScrollView
    android:id="@+id/BroadScrollView"
    android:layout_width="393dp"
    android:layout_height="505dp"
    android:layout_marginBottom="144dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@android:color/holo_red_dark"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/broad"
    app:layout_constraintVertical_bias="0.0">

    <LinearLayout
        android:id="@+id/BroadLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>

<ScrollView
    android:id="@+id/PhotoScrollView"
    android:layout_width="0dp"
    android:layout_height="433dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:background="@android:color/holo_green_dark"
    app:layout_constraintBottom_toBottomOf="@+id/BroadScrollView"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintVertical_bias="1.0"
    app:layout_constraintTop_toBottomOf="@+id/broad"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp">

    <LinearLayout
        android:id="@+id/PhotoVidLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>

我不明白为什么隐藏ScrollView的按钮给我带来了问题。

3 个答案:

答案 0 :(得分:0)

在这里你试图获得按钮:

const o = require("lodash.omit")
const foo = { [Symbol.for('a')]: 'abc', b: 'b', c: 'c' }
const bar = o(foo, 'a') // "'a' undefined"
const baz = o(foo, [ Symbol.for('a'), 'b' ]) // Symbol supported, more than one prop at a time, "Symbol.for('a') undefined"

如果您在此行之后进行检查,则activityButtonPhoto= rootView.findViewById(R.id.PhotoVid); activityButtonPhoto,这意味着找不到按钮null中标识为PhotoVid的按钮进入activity_saved_broad_tabbar

由于它为null,当您尝试设置rootView时,它会抛出空指针异常。

检查ID是否拼写正确且布局是否包含此ID。

答案 1 :(得分:0)

这是一个拼写错误。

在您的布局xml中,您的ScrollView为android:id="@+id/BroadScrollView",但您使用的是R.id.BroadcastScrollView

因此Android无法在rootView中找到此ScrollView,因此broadScroll将为null并导致NullPointerException。

答案 2 :(得分:0)

你可以试试这个吗?

public class SavedBroad extends Fragment 
{

  Button activityButtonPhoto;
  public ScrollView broadScroll;


  Button activityButtonBroad;
  public ScrollView photoScroll;


  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
  }

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


       broadScroll = rootView.findViewById(R.id.BroadScrollView); 
     // photoScroll = rootView.findViewById(R.id.PhotoScrollView);  


    //  broadScroll= getActivity().findViewById(R.id.BroadScrollView); 
    // photoScroll=getActivity().findViewById(R.id.PhotoScrollView);




      activityButtonPhoto= rootView.findViewById(R.id.PhotoVid);
      activityButtonPhoto.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {

              //broadScroll.setVisibility(View.INVISIBLE);
              broadScroll.setVisibility(View.GONE);

          }

      });

      return rootView;

  }


}