2016-07-30 15 views
-1

フラグメント内のtextViewのテキストを変更しようとしたときにnullPointerExceptionが発生しましたとgetActivity()の前に入れてみましたが、両方ともエラーが発生します。ありがとうございました。NullPointerExceptionフラグメント内のtextViewテキストを変更しようとしたとき


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) 

答えて

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; 
} 
関連する問題