尝试更改片段中的textView文本时出现NullPointerException

时间:2016-07-30 08:09:24

标签: android

当我尝试更改片段中textView的文本时,我收到了nullPointerException我尝试过将getView()getActivity()放在findByViewId之前的建议,但它们都会导致错误。谢谢。

public class MainFragment extends Fragment {


public MainFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootview = inflater.inflate(R.layout.fragment_main, container, false);

    //calls setdate function
    setDate();

    return rootview;
}

public void setDate(){
    String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
    //need to use calendar class as Date is deprecated (old)


    TextView textView = (TextView) getActivity().findViewById(R.id.info_text);
    textView.setText("updated");
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginTop="75dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Test"
            android:textSize="25dp"
            android:textStyle="bold"
            android:textColor="#252525" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view2"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="395dp"
        card_view:cardCornerRadius="4dp"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/info_text2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="placeholder_text"
            android:textSize="25dp"
            android:textStyle="bold"
            android:textColor="#252525" />
    </android.support.v7.widget.CardView>

</LinearLayout>
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                           at au.com.alexh.dayrater.MainFragment.setDate(MainFragment.java:43)
                                                                           at au.com.alexh.dayrater.MainFragment.onCreateView(MainFragment.java:33)

1 个答案:

答案 0 :(得分:0)

将您的setDate更改为:

public void setDate(View v){
    String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
    //need to use calendar class as Date is deprecated (old)


    TextView textView = (TextView) v.findViewById(R.id.info_text);
    textView.setText("updated");
}

并将其命名为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootview = inflater.inflate(R.layout.fragment_main, container, false);

    //calls setdate function
    setDate(rootview);

    return rootview;
}