2013-10-29 96 views
0

我是新來的Android,(不編程,甚至Java),所以忍受着我。Android的片段問題

我想要得到使用片段的句柄。

我有一個使用默認輕掃/操作欄創建的項目。我進一步擴展了這一點,以處理我想要的設置....但是我不太明白髮生了什麼/如何解決這個問題。

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a DummySectionFragment (defined as a static inner class 
     // below) with the page number as its lone argument. 
     Fragment fragment = new DummySectionFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public int getCount() { 
     // Show 8 total pages. 
     return 8; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
     case 0: 
      return getString(R.string.title_section1).toUpperCase(l); 
     case 1: 
      return getString(R.string.title_section2).toUpperCase(l); 
     case 2: 
      return getString(R.string.title_section3).toUpperCase(l); 
     case 3: 
      return getString(R.string.title_section4).toUpperCase(l); 
     case 4: 
      return getString(R.string.title_section5).toUpperCase(l); 
     case 5: 
      return getString(R.string.title_section6).toUpperCase(l); 
     case 6: 
      return getString(R.string.title_section7).toUpperCase(l); 
     case 7: 
      return getString(R.string.title_section8).toUpperCase(l); 
     } 
     return null; 
    } 
} 

/** 
* A dummy fragment representing a section of the app, but that simply 
* displays dummy text. 
*/ 
public class DummySectionFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public String ARG_SECTION_NUMBER = "section_number"; 

    public DummySectionFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     int position; 

     position = getArguments().getInt(ARG_SECTION_NUMBER)-1; 
     View rootView; 
     TextView dummyTextView; 

我真的不希望任何靜態或最終在這裏,我已經得到了它主要是制定了,但我不明白下面的行或如何解決它。我有點得到它在做什麼。

args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 

的錯誤是:不能讓一個靜態引用非靜態字段DummySectionFragment.ARG_SECTION_NUMBER

有可能是這一個簡單的解決,我只是感到與Android和Java夠陌生的,因爲我的目前的工作我花了我所有的時間在SQL Server。

- 編輯添加 我不反對靜態或最終等任何事情。我不是很明白的問題是當我想在每個片段中做些事情。我在每個佈局上都有一個textview,我希望能夠在循環中操作它們。我想我被困在一個圈子裏,無法想出我的出路......哈哈。

例如我把上面的代碼下面是

case 4: 
      rootView = inflater.inflate(R.layout.fragment_main_location,container, false); 
      dummyTextView= (TextView) rootView .findViewById(R.id.section_label); 

      // location 
      Button btnShowLocation = (Button) rootView.findViewById(R.id.btnShowLocation); 
      Button btnShowDBLocList = (Button) rootView.findViewById(R.id.btnShowDBLocList); 
      Button btnLocationsCount = (Button) rootView.findViewById(R.id.btnLocationsCount); 
      Button btnTruncateDBLocationsTable = (Button) rootView.findViewById(R.id.btnTruncateDBLocationsTable); 

      btnTruncateDBLocationsTable.setOnClickListener(new OnClickListener() { 
       @Override     
       public void onClick(View v) { 
        Activity activity = getActivity(); 
        int intCount = 0; 

        /*if (activity != null) { 
         //dummyTextView.setText(""); 
        try { 
         locationDatabaseHandler.truncateLocationTable(); 
         intCount = locationDatabaseHandler.getLocationCount(); 
        } catch (Exception e){ 
         //dummyTextView.append(e.toString()); 
        } 
        //dummyTextView.append("Count:" + intCount + "\n\n"); 
        Toast.makeText(activity, "toast_you_just_clicked_a_fragment btnTruncateDBLocationsTable button", Toast.LENGTH_LONG).show(); 
        }*/ 
       } 
      }); 

      dummyTextView = (TextView) rootView .findViewById(R.id.section_label); 
      dummyTextView.append("\nLocation Stuff\n"); 
      break; 

//dummyTextView.append("Count:」 + intCount + 「\ n \ n」);

我碰到一個圈子,如果我dummyTextView試圖在onClick事件中使用dummmyText w /它說我需要使它變成靜態的(快速修復),抱怨錯誤:無法引用non-在不同的方法中定義的indder類中的最終變量dummy7Text。

我已經添加了一個變量來處理onCreate內部的填充(LayoutInflater和Viewgroup,然後在onclick中引用它們(未顯示),但是當我進入並instaniate ...沒有與textviews發生...

有東西我不是很到這裏,一旦我通過這個障礙,我會有這個由球,並將能夠使它做我想做的事情。

回答

1

我真的不希望任何靜態或最終這裏

爲什麼?它們不會對性能產生負面影響,也不是糟糕的編碼實踐。

我不理解以下行

每個片段可以用含有任何數量的鍵 - 值對的捆綁來創建。DummySectionFragment.ARG_SECTION_NUMBER是一個鍵(一個字符串),並且position + 1是值。因此,這段代碼告訴新的DummySectionFragment Fragment應該顯示哪部分內容。

此方法比將這些參數放入構造函數中更可取,因爲您不能保證調用Fragment的自定義構造函數。 Android可以通過多種方式生成碎片,這樣可以降低出現NullPointerExceptions等問題的可能性。

錯誤的是:你似乎知道,DummySectionFragment.ARG_SECTION_NUMBER指的是靜態字段名爲ARG_SECTION_NUMBERDummySectionFragment類中不能使靜態參考非靜態字段DummySectionFragment.ARG_SECTION_NUMBER

。通過使該字段非靜態,您不能再引用此常數值,而不需要DummySectionFragment實例。

另一種選擇(如果你真的不想要一個靜態字段)將硬編碼字符串。因此,你的代碼將是:

args.putInt("section_number", position + 1); 

然而,公共靜態字段是一個更好的編碼實踐,並防止在你的字符串輸入錯誤愚蠢的錯誤。

我碰上了cirle在那裏,如果我dummyTextView嘗試使用dummmyText瓦特/ onClick事件,它說,我需要一個抱怨錯誤,使其靜態(快速修復):不能指在不同的方法中定義的indder類中的非final變量dummy7Text。

而不是使用匿名內部類,我會讓你的片段實現OnClickListener

例如:

public class MyFragment extends Fragment implements OnClickListener { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // ... 

     Button btnTruncateDBLocationsTable = (Button) rootView.findViewById(R.id.btnTruncateDBLocationsTable); 
     btnTruncateDBLocationsTable.setOnClickListener(this); 

     // ... 
    } 

    @Override 
    public void onClick(View v) { 
     // You can reference dummyTextView here without any problems 
    } 
} 
+0

好吧,我開始得到什麼的布萊恩,請看到我的編輯和增加進一步澄清什麼,我想要做一個輕微的手柄。我覺得我很接近。 – shaddow

+0

對於關於'OnClickListener'的其他問題,在這種情況下,我建議讓你的Fragment實現'OnClickListener'而不是嘗試使用匿名內部類。這樣你就不必擔心你的變量是靜態的還是最終的,這樣它們就可以被內部類使用。 –

+0

的例子,會有幫助嗎?我以前用Java編寫過,但它可能已經8年了......而且我正在挖掘自己的方式,我花了很多時間放牧數據庫(DBA)而不是編碼,但是補充一點,我喜歡編碼。 。 我對這些片段中的每一個都有putbuttons以允許我在另一個項目中實現的各種funcationality,並且我試圖通過一個新的接口來複制功能(也就是爲什麼我甚至在做這個atm)。 – shaddow

0

這意味着ARG_SECTION_NUMBER應該被聲明爲public static。更好的,如果它宣佈爲public static final