我创建了我的应用程序,其中包含一些互联网活动,可以从服务中加载列表中的图像。当互联网或wifi连接无法用于设备时,我想显示带有确定按钮的警告对话框。
以下是我的代码,但是当我禁用WiFi运行我的应用时,它会显示消息"unfortunately app has stopped"
。
提前致谢
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
Boolean bs_netcheck;
bs_netcheck = netCheck();
if(bs_netcheck == true)
{
displayAlert();
}
ImageView logoimage = (ImageView) findViewById(R.id.logoImage);
Picasso.with(MainActivity.this).load("http://lh5.ggpht.com/_hepKlJWopDg/TB-_WXikaYI/AAAAAAAAElI/715k4NvBM4w/s144-c/IMG_0075.JPG").into(logoimage);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ImageView layout = (ImageView)findViewById(R.id.drawerlogo);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("http://devobal.in/live");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
});
TabHost host = (TabHost)findViewById(R.id.tabHost);
ViewPager pager =(ViewPager)findViewById(R.id.pager);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("Tab One");
spec.setContent(R.id.tab1);
spec.setIndicator("TEACHING");
spec.setIndicator("TEACHING", getResources().getDrawable(R.drawable.tab_teachimg));
host.addTab(spec);
//Tab 2
spec = host.newTabSpec("Tab Two");
spec.setContent(R.id.tab2);
spec.setIndicator("RESOURCES", getResources().getDrawable(R.drawable.tab_teachimg));
host.addTab(spec);
//Tab 3
spec = host.newTabSpec("Tab Three");
spec.setContent(R.id.tab3);
spec.setIndicator("MRT", getResources().getDrawable(R.drawable.tab_teachimg));
host.addTab(spec);
tab1Data();
tab2Data();
tab3Data();
host.setCurrentTab(0);
host.setOnTabChangedListener(this);
for(int i=0;i<host.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) host.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#ffffff"));
host.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#000000"));
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
} else if (id == R.id.nav_gallery) {
Intent gallery = new Intent(getApplicationContext(), Gallery_Test.class);
startActivity(gallery);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private ArrayList<ListItem> getListData(String val ) {
final String SOAP_ACTION = "http://tempuri.org/GetTeachingItems";
final String METHOD_NAME = "GetTeachingItems";
final String NAMESPACE = "http://tempuri.org/";
final String URL = "http://alive.devobal.com/PodioWebService.asmx?op=GetTeachingItems";
ArrayList<ListItem> listMockData = new ArrayList<ListItem>();
List<String> headlines = new LinkedList<>();
Email = "dtiadmin1";
Password = "DevObal1916";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
aht.debug = true;
aht.call(SOAP_ACTION, soapEnvelope);
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
Log.v("PCount", "" + resultsRequestSOAP.getPropertyCount());
Log.v("TAG", String.valueOf(resultsRequestSOAP));
SoapObject response = (SoapObject) soapEnvelope.getResponse();
SoapObject obj1, obj2, obj3, obj4;
obj1 = (SoapObject) response.getProperty("diffgram");
obj2 = (SoapObject) obj1.getProperty("NewDataSet");
//SrtVv="";
for (int j = 0; j < obj2.getPropertyCount(); j++) {
obj4 = (SoapObject) obj2.getProperty(j);
ListItem newsData = new ListItem();
newsData.setUrl(obj4.getProperty(3).toString());
newsData.setHeadline(obj4.getProperty(1).toString());
newsData.setReporterName(obj4.getProperty(2).toString());
newsData.setDate(obj4.getProperty(0).toString());
listMockData.add(newsData);
}
SoapObject res = (SoapObject) response.getProperty(0);
Response = res.toString();
String strV = response.getProperty(0).toString();
Log.v("TAG", response.toString());
Log.v("TAG", res.toString());
return listMockData;
//return nameValues;
// Log.v("TAG", com.WsApp.adapters.Constants.name);
} catch (MalformedJsonException e) {
//e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void tab1Data(){
listData = getListData("");
final ListView listView = (ListView) findViewById(R.id.custom_list);
CustomListAdapter customListAdapter = new CustomListAdapter(this,listData);
listView.setAdapter(customListAdapter);
customListAdapter.setListData(getListData(""));
customListAdapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ListItem newsData = (ListItem) listView.getItemAtPosition(position);
final TextView itemidtemp = (TextView) findViewById(R.id.tempid);
itemidtemp.setText(newsData.getDate());
// Toast.makeText(MainActivity.this, "Selected :" + " " + newsData.getDate(), Toast.LENGTH_LONG).show();
//listView.setVisibility(View.INVISIBLE);
Intent series = new Intent(MainActivity.this, SeriesActivity.class);
series.putExtra("itemid", itemidtemp.getText());
series.putExtra("title",listData.get(position).getHeadline());
startActivity(series);
// secondclick();
}
});
}
public void tab2Data() {
listData2 = getListData2("");
Log.v("bbbbbbbbb","bbbbbb"+listData2.size());
final GridView gridView = (GridView) findViewById(R.id.custom_list2);
CustomListAdapter customListAdapter = new CustomListAdapter(this,listData2);
gridView.setAdapter(customListAdapter);
customListAdapter.setListData(listData2);
customListAdapter.notifyDataSetChanged();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ListItem newsData = (ListItem) gridView.getItemAtPosition(position);
final TextView itemidtemp = (TextView) findViewById(R.id.tempid);
itemidtemp.setText(newsData.getDate());
// Toast.makeText(MainActivity.this, "Selected :" + " " + newsData.getDate(), Toast.LENGTH_LONG).show();
//listView.setVisibility(View.INVISIBLE);
Intent series = new Intent(MainActivity.this, SeriesActivity.class);
series.putExtra("itemid", itemidtemp.getText());
series.putExtra("title",listData2.get(position).getHeadline());
startActivity(series);
// secondclick();
}
});
}
private ArrayList<ListItem> getListData2(String val) {
final String SOAP_ACTION = "http://tempuri.org/GetTeachingItems";
final String METHOD_NAME = "GetTeachingItems";
final String NAMESPACE = "http://tempuri.org/";
final String URL = "http://alive.devobal.com/PodioWebService.asmx?op=GetTeachingItems";
Log.v("bbbbbbbbb","getdata2");
ArrayList<ListItem> listMockData = new ArrayList<ListItem>();
List<String> headlines = new LinkedList<>();
Email = "dtiadmin1";
Password = "DevObal1916";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
aht.debug = true;
aht.call(SOAP_ACTION, soapEnvelope);
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
Log.v("PCount", "" + resultsRequestSOAP.getPropertyCount());
Log.v("TAG", String.valueOf(resultsRequestSOAP));
SoapObject response = (SoapObject) soapEnvelope.getResponse();
SoapObject obj1, obj2, obj3, obj4;
obj1 = (SoapObject) response.getProperty("diffgram");
obj2 = (SoapObject) obj1.getProperty("NewDataSet");
//SrtVv="";
for (int j = 0; j < obj2.getPropertyCount(); j++) {
obj4 = (SoapObject) obj2.getProperty(j);
ListItem newsData = new ListItem();
newsData.setUrl(obj4.getProperty(3).toString());
newsData.setHeadline(obj4.getProperty(1).toString());
newsData.setReporterName(/*obj4.getProperty(2).toString()*/"bnr");
newsData.setDate(obj4.getProperty(0).toString());
listMockData.add(newsData);
}
SoapObject res = (SoapObject) response.getProperty(0);
Response = res.toString();
String strV = response.getProperty(0).toString();
Log.v("TAG", response.toString());
Log.v("TAG", res.toString());
return listMockData;
//return nameValues;
// Log.v("TAG", com.WsApp.adapters.Constants.name);
} catch (MalformedJsonException e) {
//e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void tab3Data(){
listData1 = getListData1("");
Log.v("aaaaaaaaa","aaaaaa"+listData1.size());
final ListView listView = (ListView) findViewById(R.id.custom_list1);
CustomListAdapter customListAdapter =new CustomListAdapter(this,listData1);
listView.setAdapter(customListAdapter);
customListAdapter.setListData(listData1);
customListAdapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ListItem newsData = (ListItem) listView.getItemAtPosition(position);
final TextView itemidtemp = (TextView) findViewById(R.id.tempid);
itemidtemp.setText(newsData.getDate());
// Toast.makeText(MainActivity.this, "Selected :" + " " + newsData.getDate(), Toast.LENGTH_LONG).show();
//listView.setVisibility(View.INVISIBLE);
Intent series = new Intent(MainActivity.this, SeriesActivity.class);
series.putExtra("itemid", itemidtemp.getText());
series.putExtra("title",listData1.get(position).getHeadline());
startActivity(series);
// secondclick();
}
});
}
private ArrayList<ListItem> getListData1(String val) {
final String SOAP_ACTION = "http://tempuri.org/GetMRTItems";
final String METHOD_NAME = "GetMRTItems";
final String NAMESPACE = "http://tempuri.org/";
final String URL = "http://alive.devobal.com/PodioWebService.asmx?op=GetMRTItems";
Log.v("aaaaaaaaa","getdata1");
ArrayList<ListItem> listMockData = new ArrayList<ListItem>();
List<String> headlines = new LinkedList<>();
Email = "dtiadmin1";
Password = "DevObal1916";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
aht.debug = true;
aht.call(SOAP_ACTION, soapEnvelope);
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
Log.v("PCount", "" + resultsRequestSOAP.getPropertyCount());
Log.v("TAG", String.valueOf(resultsRequestSOAP));
SoapObject response = (SoapObject) soapEnvelope.getResponse();
SoapObject obj1, obj2, obj3, obj4;
obj1 = (SoapObject) response.getProperty("diffgram");
obj2 = (SoapObject) obj1.getProperty("NewDataSet");
//SrtVv="";
for (int j = 0; j < obj2.getPropertyCount(); j++) {
obj4 = (SoapObject) obj2.getProperty(j);
ListItem newsData = new ListItem();
newsData.setUrl(obj4.getProperty(3).toString());
newsData.setHeadline(obj4.getProperty(1).toString());
newsData.setReporterName(obj4.getProperty(2).toString()/*"nnb"*/);
newsData.setDate(obj4.getProperty(0).toString());
listMockData.add(newsData);
}
SoapObject res = (SoapObject) response.getProperty(0);
Response = res.toString();
String strV = response.getProperty(0).toString();
Log.v("TAG", response.toString());
Log.v("TAG", res.toString());
return listMockData;
//return nameValues;
// Log.v("TAG", com.WsApp.adapters.Constants.name);
} catch (MalformedJsonException e) {
//e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private ArrayList<ListItem> getListDataSelectedItem(String val) {
String METHOD_NAME1 = "GetTeachingSeries";
String NAMESPACE1 = "http://tempuri.org/";
String URL1 = "http://alive.devobal.com/PodioWebService.asmx?op=GetTeachingSeries";
String SOAP_ACTION1 = "http://tempuri.org/GetTeachingSeries";
ArrayList<ListItem> listMockData = new ArrayList<ListItem>();
List<String> headlines = new LinkedList<>();
Email = "dtiadmin1";
Password = "DevObal1916";
SoapObject request = new SoapObject(NAMESPACE1, METHOD_NAME1);
request.addProperty("itemId", val.toString());
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL1);
try {
aht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
aht.debug = true;
aht.call(SOAP_ACTION1, soapEnvelope);
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
Log.v("PCount", "" + resultsRequestSOAP.getPropertyCount());
Log.v("TAG", String.valueOf(resultsRequestSOAP));
SoapObject response = (SoapObject) soapEnvelope.getResponse();
//Response = response.toString();
SoapObject obj1, obj2, obj3, obj4;
obj1 = (SoapObject) response.getProperty("diffgram");
obj2 = (SoapObject) obj1.getProperty("NewDataSet");
//SrtVv="";
for (int j = 0; j < obj2.getPropertyCount(); j++) {
obj4 = (SoapObject) obj2.getProperty(j);
ListItem newsData = new ListItem();
newsData.setUrl("http://lh5.ggpht.com/_hepKlJWopDg/TB-_WXikaYI/AAAAAAAAElI/715k4NvBM4w/s144-c/IMG_0075.JPG");
newsData.setHeadline(obj4.getProperty(1).toString());
newsData.setReporterName(obj4.getProperty(2).toString());
newsData.setDate(obj4.getProperty(0).toString());
listMockData.add(newsData);
}
SoapObject res = (SoapObject) response.getProperty(0);
Response = res.toString();
String strV = response.getProperty(0).toString();
Log.v("TAG", response.toString());
Log.v("TAG", res.toString());
return listMockData;
//return nameValues;
// Log.v("TAG", com.WsApp.adapters.Constants.name);
} catch (MalformedJsonException e) {
//e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
// @Override
// public void onPageSelected(int position) {
// }
// @Override
// public void onPageSelected(int position) {
// }
@Override
public void onPageScrollStateChanged(int state) {
}
/* private class MyPagerAdapter extends PagerAdapter {
private final Context context;
public MyPagerAdapter(Context context) {
this.context= context;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TextView tView = new TextView(context);
position++;
tView.setText("Page number: " + position);
tView.setTextColor(Color.RED);
tView.setTextSize(20);
container.addView(tView);
return tView;
}
@Override
public int getCount() {
return 3;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
} */
public boolean netCheck(){
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
boolean bs_netcheck = false ;
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
//notify user you are not online
bs_netcheck =true;
}
return bs_netcheck;
}
public void displayAlert()
{
new AlertDialog.Builder(this).setMessage("Please Check Your Internet Connection and Try Again")
.setTitle("Network Error")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
}
}