gpt4 book ai didi

java - 使用 ViewPager 从 Activity 更改 Fragment TextView

转载 作者:行者123 更新时间:2023-12-02 06:19:11 25 4
gpt4 key购买 nike

我有一个 Activity ,可以在单击按钮后从 Web API 检索数据并将其保存在自定义统计对象中。在我看来,我有一个带有 ViewPager 的 TabLayout。问题是,在 Activity 检索所有数据后,我无法更改 Fragment 中的 TextView,我总是收到 NullPointerException。

我尝试了很多方法,但似乎都不起作用,我不知道该怎么办。

这是我的 Activity :


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);
}
}
}

我的 fragment 代码:


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,我不知道如何解决这个问题,希望有人能帮助我!谢谢!

最佳答案

在 onPostExecute 方法中使用它

      //HERE IS WHERE I WANT TO CHANGE MY FRAGMENT TEXTVIEW        
if (pageAdapter.getFragment() != null) {
pageAdapter.getFragment().getStats(soloStats);
}

关于java - 使用 ViewPager 从 Activity 更改 Fragment TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com