2015-02-12 45 views
0

我想要一個TextView總是說當前的日期。但是,setText方法使用NullPointerExeption崩潰程序...截至目前,日期不是問題 - 我只使用常規文本來測試它。我不知道放置代碼的最佳位置,因此我決定使用onActivityCreated。怎麼回事,還有什麼我可以做的,讓我的代碼更好? *請注意,textview位於fragment_page中,而不是fragment_page 2,因爲這是滑動標籤佈局中的第一個標籤。爲什麼在一個片段中設置textview文本會導致崩潰?

片段類

public class PageFragment extends Fragment { 
public static final String ARG_PAGE = "ARG_PAGE"; 

private int mPage; 

public static PageFragment newInstance(int page) { 
    Bundle args = new Bundle(); 
    args.putInt(ARG_PAGE, page); 
    PageFragment fragment = new PageFragment(); 
    fragment.setArguments(args); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mPage = getArguments().getInt(ARG_PAGE); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view; 
    if(mPage < 2) { 
     view = inflater.inflate(R.layout.fragment_page, container, false); 
    } else { 
     view = inflater.inflate(R.layout.fragment_page_2, container, false); 
    } 


    return view; 
} 

//@Override 
public void onActivityCreated(Bundle savedInstanceState){ 
    super.onActivityCreated(savedInstanceState); 
    /*Calendar calendar = Calendar.getInstance(); 
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy"); 


     */ 
     TextView textView = (TextView)  getView().findViewById(R.id.current_date); 
     textView.setText("sadFace"); //null pointer exeption on this line 
    } 
} 

片段XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingLeft="16dp" 
      android:paddingRight="16dp" 
      android:paddingTop="16dp" > 
      <ImageView 
       android:layout_width="239dp" 
       android:id="@+id/logo" 
       android:layout_height="100dp" 
       android:src="@drawable/logo" 
       android:layout_alignParentStart="true" 
       android:layout_alignParentLeft="true"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/current_date" 
       android:text="Today is January 1st, 2015" 
       android:layout_toRightOf="@id/logo" 
       android:gravity="center" 
       android:layout_alignBottom="@id/logo"/> 

     </RelativeLayout> 
</LinearLayout> 

回答

1

的問題是在該行

TextView textView = (TextView) getView().findViewById(R.id.current_date);

其工作本身,而是導致空指針。我將getView()更改爲getActivity(),現在它完美地工作。

+1

謝謝,它爲我工作! – Carl 2016-03-09 06:57:50

0

onCreateView創建並返回與該片段相關聯的視圖的層次結構。

如果current_date只在fragment_page中,則將代碼塊移動到onCreateView之內,那麼您還應該將它們移動到IF的條件中。

View view; 
    if(mPage < 2) { 
     view = inflater.inflate(R.layout.fragment_page, container, false); 
     TextView textView = (TextView)view.findViewById(R.id.current_date); 
     textView.setText("sadFace"); 
    } else { 
     view = inflater.inflate(R.layout.fragment_page_2, container, false); 
    } 
+0

我之前在那裏,因爲它崩潰了,我把它移到片段生命週期的更遠處去看。然而,現在再做一遍,我注意到崩潰發生在TextView實例化行,而不是textView.setText ...仍然是空指針豁免。 – 2015-02-12 02:14:49

+0

current_date在layout_page_page右邊?你可以做一個調試,看看你的觀點通脹實際上是否返回任何東西?如果您的崩潰發生在findViewById行,則可能意味着您的視圖爲空。 – SteD 2015-02-12 02:20:42

+0

視圖?它確實會返回一些東西(儘管TextView對它沒有影響......)在評論Textview時,一切都按預期工作。上面的方式,只是評論setText作品。在這裏,我必須評論textView本身。發生錯誤的地方是否有改變的原因? – 2015-02-12 02:29:38

相關問題