2015-06-28 37 views
2

我寫的,所用日曆的活動類的代碼,然後我把它移到片段,和我動態連接該片段到活動中,通過調用:`日曆代碼工作的活動,但不是在片段

mFragmentManager = getFragmentManager(); 

     //mButtonsFragment = mFragmentManager.findFragmentById(R.id.fragment_buttons); 
     mLayout1to6 = (FrameLayout) findViewById(R.id.fragment_1to6); 
     final FragmentTransaction ft = mFragmentManager.beginTransaction(); 

     ft.add(R.id.fragment_1to6, f1to6); 
     ft.addToBackStack(null); 
     ft.commit(); 

     mFragmentManager 
       .addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { 
        public void onBackStackChanged() { 
         setLayout(); 
        } 
       }); 


     mFragmentManager.executePendingTransactions(); 
` 

和片段代碼本身是這樣的:

public class Fragment_1to6 extends Fragment { 
    private Activity mActivity; 

    Calendar calendar; 
    String month_name; 

    TextView month1; 
    TextView month2; 
    TextView month3; 
    TextView month4; 
    TextView month5; 

    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     mActivity = activity; 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_fragment_1to6, container, false); 
     month1 = (TextView) mActivity.findViewById(R.id.monthView1); 
     month2 = (TextView) mActivity.findViewById(R.id.monthView2); 
     month3 = (TextView) mActivity.findViewById(R.id.monthView3); 
     month4 = (TextView) mActivity.findViewById(R.id.monthView5); 

     calendar = Calendar.getInstance(); 
     setMonths(); 

      return view; 

     } 
    public void setCurrentMonthName() { 

     SimpleDateFormat month_date = new SimpleDateFormat("MMM"); 
     month_name = month_date.format(calendar.getTime()); 
     month1.setText(month_name); 
    } 


    protected void getNextMonth(int i) { 

     SimpleDateFormat dateFormat = new SimpleDateFormat("MMM"); 

     switch (i) { 
      case 1: 
       calendar.add(Calendar.MONTH, 1); 
       month2.setText(dateFormat.format(calendar.getTime())); 
       break; 
      case 2: 
       calendar.add(Calendar.MONTH, 1); 
       month3.setText(dateFormat.format(calendar.getTime())); 
       break; 
      case 3: 
       calendar.add(Calendar.MONTH, 1); 
       month4.setText(dateFormat.format(calendar.getTime())); 
       break; 
      case 4: 
       calendar.add(Calendar.MONTH, 1); 
       month5.setText(dateFormat.format(calendar.getTime())); 
       break; 
     } 

    } 
    public void setMonths() { 
     setCurrentMonthName(); 
     getNextMonth(1); 
     getNextMonth(2); 
     getNextMonth(3); 
     getNextMonth(4); 
    } 


} 
` 

然而,當我運行它,它給人的NullPointerException在哪裏我就textViews的setText行了,說不能上的setText空對象引用。

回答

1

你在哪裏就可以充氣的片段替換此代碼

month1 = (TextView) mActivity.findViewById(R.id.monthView1); 
month2 = (TextView) mActivity.findViewById(R.id.monthView2); 
month3 = (TextView) mActivity.findViewById(R.id.monthView3); 
month4 = (TextView) mActivity.findViewById(R.id.monthView5); 

month1 = (TextView) view.findViewById(R.id.monthView1); 
month2 = (TextView) view.findViewById(R.id.monthView2); 
month3 = (TextView) view.findViewById(R.id.monthView3); 
month4 = (TextView) view.findViewById(R.id.monthView5); 

活動中。但是所有的TextView都是片段xml的一部分,你必須從片段視圖訪問它們,而不是從活動視圖訪問它們。

+0

謝謝!我還將FrameLayout更改爲xml中的片段 –

相關問題