2016-09-18 48 views
2

這是我的片段類:爲什麼在AsyncTask中查看片段爲空?

公共類StanzeFragment擴展片段{

public StanzeFragment(){} 

Button vis_stanze; 
public TextView testo; 

View rootView; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    rootView = inflater.inflate(R.layout.activity_log_stanze, container, false); 
    testo = (TextView) rootView.findViewById(R.id.testo); 

    vis_stanze = (Button)rootView.findViewById(R.id.bottone_stanze); 
    vis_stanze.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      getStanze backgroundTask = new getStanze(); 
      backgroundTask.execute(); 
     } 
    }); 
    return rootView; 
    } 

public void setTesto(String value){ 
    testo.setText(value); 
    } 
    } 

但現在我需要調用其他類(的AsyncTask類)方法 「setTesto」,在onPostExecute方法:

@Override 
    protected void onPostExecute(String result) { 
     StanzeFragment test = new StanzeFragment(); 
     test.setTesto(result); 
    } 

他給我這個錯誤:

「顯示java.lang.NullPointerException:試圖調用虛擬方法void com.androidnuovosa.bocci.navdriwerdb.Utente.StanzeFragment.setTesto(java.lang.String)'在一個空對象引用「.. ..我想因爲他沒有看到片段的視圖,我該如何解決這個問題問題? 需要回答的問題。 (我有這個問題,其他類也是如此)

編輯:

的AsyncTask不在StanzeFragment

回答

2

Fragments應該由系統實例化(因爲它們是在佈局XML中定義的),或者通過使用FragmentManager方法從代碼實例化。

如果你只是實例化一個StanzeFragment這樣,它實際上並沒有出現在視圖堆棧和testoTextViewnull這個似乎是你的問題。

在你的情況下,當AsyncTask運行時,顯然Fragment已經可見,你應該只是得到該Fragment實例的引用,而不是創建一個新實例。

FragmentManager有方法找到一個Fragment或者由id(如在XML定義)或由tag(給定的表示編程它時):

findFragmentById()

findFragmentByTag()

編輯:

在您的佈局XML爲Activity你可能有一個Fragment

<fragment 
    android:name="com.example.MyFragment" 
     android:id="@+id/my_fragment" 
    ... /> 

現在根據您的應用程序的確切配置(Activity類型實際上),你會調用其中:

getFragmentManager()

...或...

getSupportFragmentManager()

...等等,例如:

FragmentManager fm = getFragmentManager(); 

訪問FragmentManager。然後你用它的ID my_fragment找到你MyFragment

MyFragment myFrag = (MyFragment)fm.findFragmentById(R.id.my_fragment); 

這應該都在其中包含上述FragmentActivity來完成。

保持對片段引用的另一種(替代)方法是在Activity中實現onAttachFragment()方法。 (在Activity其中將包含所述Fragment):

@Override 
public void onAttachFragment(Fragment fragment) { 
    super.onAttachFragment(fragment); 

    if (fragment instanceof StanzeFragment) { 
     stanzeFragment = (StanzeFragment) fragment; 
    } 
} 

onAttachFragment()方法是RAN當你的Fragment被構造和被系統顯示。在documentation確切的寫法是

Called when a Fragment is being attached to this activity, immediately after the call to its Fragment.onAttach() method and before Fragment.onCreate().

所以在這一點上Fragment沒有完全準備好,但這個反正到達這實際上將出現在設備屏幕上的正確Fragment實例的引用。所以當你以後在你的代碼中使用stanzeFragment時,你可以訪問它的TextViews和其他東西。

+0

嗨,tnks爲答案。你能告訴我一個例子嗎?我創建了一個片段的對象:'StanzeFragment test = new StanzeFragment();'現在呢?我需要的? –

+0

我添加了一些示例代碼。 –

+0

但我有一個問題,我的片段不在這個活動中,是在另一個活動..事實上,在這個活動中我沒有<片段..... /> ..我怎樣才能調用片段屬於其他活動?如果我用StanzeFragment佈局創建他開始合併這兩個片段 –

0

如果你執行你的StanzeFragment裏面的AsyncTask,你只是這樣做:

@Override 
protected void onPostExecute(String result) { 
    setTesto(result); 
} 
+0

這是問題,asyncTask不在StanzeFragment –

+0

內我可以看到,點擊'vis_stanze'時,執行AsyncTask。我的建議應該解決你的問題。 – user1506104

+0

Mmh是的我解決這個問題與你的答案,但在其他班級我有同樣的問題..你不知道爲什麼所有的視圖值爲空? –