我制作了sharmuktakkhand .java文件。
public class sharmuktakkhand extends AppCompatActiyvit {
private final List<String> devices1 = new ArrayList<>();
private final List<String> deviceDescription1 = new ArrayList<>();
public String[] devices;
public String[] deviceDescription;
public int pos;
private ProgressDialog loading;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_geet);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getData();
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void showJSON(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(config.JSON_ARRAY);
for (int i = 0; i < result.length(); i++) {
JSONObject collegeData = result.getJSONObject(i);
devices1.add(collegeData.getString(config.KEY_NAME));
deviceDescription1.add(collegeData.getString(config.KEY_ADDRESS));
}
} catch (JSONException e) {
e.printStackTrace();
}
for(int i =0;i<devices1.size();i++){
Toast.makeText(sharmuktakkhand.this, devices1.get(i), Toast.LENGTH_LONG).show();
devices[i] = devices1.get(i);
deviceDescription[i] = deviceDescription1.get(i);
}
}
private void getData() {
loading = ProgressDialog.show(sharmuktakkhand.this, "Please wait...", "Fetching...", false, false);
String url = config.DATA_URL;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(sharmuktakkhand.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(sharmuktakkhand.this);
requestQueue.add(stringRequest);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
pos = position;
bundle.putString(kavitaFragment.DevicesKey, devices[position]);
bundle.putString(kavitaFragment.DescriptionKey, deviceDescription[position]);
kavitaFragment deviceFragment = new kavitaFragment();
deviceFragment.setArguments(bundle);
return deviceFragment;
}
public void findposition(int posio ){
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_SUBJECT, "My cool app");
i.putExtra(android.content.Intent.EXTRA_TEXT, devices[posio] + deviceDescription[posio] + "\nN.D.Nimbawat\nAdvocate");
startActivity(Intent.createChooser(i, "Share"));
return ;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public int getCount() {
return devices.length;
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
super.destroyItem(container, position, object);
}
}
private void setupViewPager(ViewPager viewPager) {
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
for(int i = 1; i<=devices.length ; i++) {
adapter.addFragment(new kavitaFragment(), "shar muktak khand - "+ i );
}
viewPager.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
case R.id.action_share:
SectionsPagerAdapter mspa = new SectionsPagerAdapter(getSupportFragmentManager());
mspa.findposition(viewPager.getCurrentItem());
return true;
}
return super.onOptionsItemSelected(item);
}
}
和config.java文件
public static final String DATA_URL = "http://m3microtech.com/getData.php";
public static final String KEY_NAME = "title";
public static final String KEY_ADDRESS = "detail";
public static final String JSON_ARRAY = "result";
我使用JSON数组获取数据并将此数据放入device1(listArray)
问题是 - 片段不能及时创建(首先使用片段包,然后使用数组)
我的应用停止而不是运行。
请帮帮我
感谢提前
答案 0 :(得分:0)
如果您没有数据等待数据,请不要设置setupViewPager(viewPager);
,然后setupViewPager(viewPager);
将setupViewPager(viewPager);
移动到json的响应部分,如下所示。
private void showJSON(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(config.JSON_ARRAY);
for (int i = 0; i < result.length(); i++) {
JSONObject collegeData = result.getJSONObject(i);
devices1.add(collegeData.getString(config.KEY_NAME));
deviceDescription1.add(collegeData.getString(config.KEY_ADDRESS));
}
} catch (JSONException e) {
e.printStackTrace();
}
for(int i =0;i<devices1.size();i++){
Toast.makeText(sharmuktakkhand.this, devices1.get(i), Toast.LENGTH_LONG).show();
devices[i] = devices1.get(i);
deviceDescription[i] = deviceDescription1.get(i);
}
// set here when your data is ready and also add check if data is null and show toast or your logic?
setupViewPager(viewPager);
}