我製作了sharmuktakkhand .java文件。無法在我的Android應用程序中顯示json數據
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)
問題是 - 片段不首先創建及時(片段箱然後陣列使用)
我的應用程序停止並未運行。
請幫我
感謝提前
發佈您在logcat上報告的錯誤。 – Arshak
添加您的logcat錯誤 – vinoth12594
從服務器獲取數據後設置viewpager和選項卡布局。 –