Reputation: 51
I am doing a google map project. My requirement is if I press a button in the present activity, it will take to google map activity else it should show the alert dialog showing the network not available message. The app crashes if there is no network. The Following is the code:
public class HomeFragment extends Fragment {
protected static final Context Context = null;
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd; public HomeFragment(){}
ImageButton button; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
@Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState);
// creating connection detector class instance
cd = new ConnectionDetector( getActivity());
button = (ImageButton) getActivity().findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
// TODO Auto-generated method stub
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
Intent i = new Intent(getActivity(), GoogleActivity.class);
startActivity(i);
} else {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(Context , "No Internet Connection" , "You don't have internet connection.", true);
}
} });
}
public void showAlertDialog(Context HomeFragment, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(HomeFragment).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
"OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
// Showing Alert Message
alertDialog.show();
} }
Logcat
01-06 16:22:48.940: W/dalvikvm(4223): threadid=1: thread exiting with uncaught exception (group=0x40de4540)
01-06 16:22:48.960: E/AndroidRuntime(4223): FATAL EXCEPTION: main
01-06 16:22:48.960: E/AndroidRuntime(4223): java.lang.NullPointerException
01-06 16:22:48.960: E/AndroidRuntime(4223): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
01-06 16:22:48.960: E/AndroidRuntime(4223): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
01-06 16:22:48.960: E/AndroidRuntime(4223): at com.ons.bhoomi.HomeFragment.showAlertDialog(HomeFragment.java:90)
01-06 16:22:48.960: E/AndroidRuntime(4223): at com.ons.bhoomi.HomeFragment$1.onClick(HomeFragment.java:77)
01-06 16:22:48.960: E/AndroidRuntime(4223): at android.view.View.performClick(View.java:4102)
01-06 16:22:48.960: E/AndroidRuntime(4223): at android.view.View$PerformClick.run(View.java:17085)
01-06 16:22:48.960: E/AndroidRuntime(4223): at android.os.Handler.handleCallback(Handler.java:615)
01-06 16:22:48.960: E/AndroidRuntime(4223): at android.os.Handler.dispatchMessage(Handler.java:92)
01-06 16:22:48.960: E/AndroidRuntime(4223): at android.os.Looper.loop(Looper.java:155)
01-06 16:22:48.960: E/AndroidRuntime(4223): at android.app.ActivityThread.main(ActivityThread.java:5520)
01-06 16:22:48.960: E/AndroidRuntime(4223): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 16:22:48.960: E/AndroidRuntime(4223): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 16:22:48.960: E/AndroidRuntime(4223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1058)
01-06 16:22:48.960: E/AndroidRuntime(4223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
01-06 16:22:48.960: E/AndroidRuntime(4223): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 359
Reputation: 22945
Try this way,
public class HomeFragment extends Fragment {
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
ImageButton button;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// creating connection detector class instance
cd = new ConnectionDetector(getActivity());
button = (ImageButton) getActivity().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
Intent i = new Intent(getActivity(), GoogleActivity.class);
startActivity(i);
} else {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(getActivity(), "No Internet Connection", "You don't have internet connection.", true);
}
}
});
}
public void showAlertDialog(Context HomeFragment, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(HomeFragment).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
"OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
// Showing Alert Message
alertDialog.show();
}
}
Upvotes: 0
Reputation: 11545
Your Context is null initialize it, add this peice of code in onActivityCreated
Context = getActivity().getApplicationContext();
Upvotes: 0
Reputation: 5636
Two changes
so it will be
AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeFragment); //Here i doubt instead of HomeFragment it should be getActivity()
Call alertDialog.create() ones you are done with dialog setup:-
alertDialog.create().show();
Upvotes: 2