我有一个Activity,单击一个按钮后,该活动将从Web API检索数据并将其保存在自定义Stats对象中。在我看来,我有一个带有ViewPager的TabLayout。问题是,在Activity检索完所有数据后,我无法从Fragment更改TextView,我总是会收到NullPointerException。
我尝试了很多事情,但似乎都没有作用,我也不知道该怎么办。
这是我的活动:
public class StatsActivity extends AppCompatActivity {
public TextView textStats, responseView;
public EditText usuario;
public ProgressBar progressBar;
public ImageButton buscar;
public TabLayout tabLayout;
public TabItem tabSolo, tabDuo, tabSquad;
public ViewPager viewPager;
String idUsuario, user;
Stats soloStats = new Stats();
Stats duoStats = new Stats();
Stats squadStats = new Stats();
Typeface TF;
PageAdapter pageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats);
textStats = findViewById(R.id.textStats);
progressBar = findViewById(R.id.progressBar);
responseView = findViewById(R.id.responseView);
usuario = findViewById(R.id.editText);
buscar = findViewById(R.id.searchButton);
tabLayout = findViewById(R.id.tabs);
tabSolo = findViewById(R.id.tabsolo);
tabDuo = findViewById(R.id.tabduo);
tabSquad = findViewById(R.id.tabsquad);
viewPager = findViewById(R.id.pager);
TF = Typeface.createFromAsset(getAssets(), "font/BurbankBigCondensed-Bold.ttf");
pageAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pageAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setVisibility(View.INVISIBLE);
setCustomFont();
LinearLayout tabStrip = ((LinearLayout) tabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
tabLayout.setVisibility(View.INVISIBLE);
responseView.setText("");
responseView.setMovementMethod(new ScrollingMovementMethod());
textStats.setTypeface(TF);
responseView.setTypeface(TF);
buscar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
user = usuario.getText().toString().trim();
new getPlayerId().execute();
}
});
}
class getPlayerId extends AsyncTask<Void, Void, String> {
private Exception exception;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
responseView.setText("");
}
protected String doInBackground(Void... urls) {
user = user.trim();
try {
// getting data
return stringBuilder.toString();
}
finally{
urlConnection.disconnect();
}
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
}
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
Log.i("INFO", response);
try {
JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
//Parsing JSON and setting soloStats
} catch (JSONException e) {
e.printStackTrace();
}
//HERE IS WHERE I WANT TO CHANGE MY FRAGMENT TEXTVIEW
//SoloFragment solo = (SoloFragment) pageAdapter.getItem(0);
//solo.getStats(soloStats);
}
}
}
我的片段代码:
public class SoloFragment extends Fragment {
TextView textVictorias, textTop2, textTop3, textPartidas, textKills, textPuntos, textMinutos, textJugadores;
public SoloFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_solo, container, false);
textVictorias = v.findViewById(R.id.textVictorias);
textTop2 = v.findViewById(R.id.textTop2);
textTop3 = v.findViewById(R.id.textTop3);
textPartidas = v.findViewById(R.id.textPartidas);
textKills = v.findViewById(R.id.textKills);
textPuntos = v.findViewById(R.id.textPuntos);
textMinutos = v.findViewById(R.id.textMinutos);
textJugadores = v.findViewById(R.id.textJugadores);
return v;
}
public void setStats(Stats stats){
textVictorias.setText(stats.getTop1());
textTop2.setText(stats.getTop2());
textTop3.setText(stats.getTop3());
textPartidas.setText(stats.getPartidas());
textKills.setText(stats.getKills());
textPuntos.setText(stats.getPuntos());
textMinutos.setText(stats.getMinutos());
textJugadores.setText(stats.getJugadores());
}
}
我总是在setStats中的setText上获得NPE,但我不知道如何解决这个问题,希望有人能帮助我!谢谢!
答案 0 :(得分:0)
在您的onPostExecute方法中使用它
//HERE IS WHERE I WANT TO CHANGE MY FRAGMENT TEXTVIEW
if (pageAdapter.getFragment() != null) {
pageAdapter.getFragment().getStats(soloStats);
}