我重复this问题的回答,失败了。 我需要更改位于片段中的cardView中的textView(文本)。这是Fragment的code,我想在方法 updateTextView 中更改文本,但我崩溃了。 这是我用Activity调用方法的代码
private void makeView(String wallPhoto, String wallText){
Fragment fr = TabFragment1.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.tab_fragment_1, fr, "tag").commit();
TabFragment1 tabFragment1_text = (TabFragment1) getSupportFragmentManager().findFragmentByTag("tag");
tabFragment1_text.updateTextView("walll");
Log.d("", wall_text);
}
崩溃似乎是:java.lang.NullPointerException: Attempt to invoke virtual method 'void pc.dd.tabhost.TabFragment1.updateTextView(java.lang.String)' on a null object reference
更新:
如果按照Nfear的建议,现在没有错误,但文本不会改变。像这样:
private void makeView(String wallPhoto, String wallText){
Fragment fr = TabFragment1.newInstance();
getSupportFragmentManager(). beginTransaction().replace(R.id.tab_fragment_1, fr, "tag").commit();
getSupportFragmentManager().executePendingTransactions();
TabFragment1 tabFragment1_text = (TabFragment1) getSupportFragmentManager().findFragmentByTag("tag");
tabFragment1_text.updateTextView("walll");
Log.d("", wall_text);
}
调用Activity asynktask,如下所示:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!VKSdk.onActivityResult(requestCode, resultCode, data, new VKCallback<VKAccessToken>() {
@Override
public void onResult(VKAccessToken res) {
_VKLogin = true;
Toast.makeText(getApplicationContext(),"Login succes ",Toast.LENGTH_LONG).show();
VKRequestResponse _response = new VKRequestResponse();
_response.execute(res);
}
@Override
public void onError(VKError error) {
_VKLogin = false;
Toast.makeText(getApplicationContext(),"Login Error",Toast.LENGTH_LONG).show();
}
}))
super.onActivityResult(requestCode, resultCode, data);
}
class VKRequestResponse extends AsyncTask{
@Override
protected Object doInBackground(Object[] params1) {
VKParameters _params = new VKParameters();
_params.put(VKApiConst.COUNT, "1");
VKRequest _vkRequest = VKApi.wall().get(_params);
_vkRequest.executeWithListener(new VKRequest.VKRequestListener() {
@Override
public void onComplete(VKResponse response) {
parsingFromJson(response.responseString);
makeView(null, wall_text);
Toast.makeText(getApplicationContext(),"Otvet "+response.responseString,Toast.LENGTH_LONG).show();
super.onComplete(response);
}
@Override
public void onError(VKError error) {
Toast.makeText(getApplicationContext(),"Ошибка "+error,Toast.LENGTH_LONG).show();
super.onError(error);
}
});
return null;
}
}