2016-09-18 50 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是的我解决这个问题与你的答案,但在其他班级我有同样的问题..你不知道为什么所有的视图值为空? –