I am working with an android database oriented program. This activity is from one of my tab. When i press that particular tab it will get the value from the db and if any null values from the db it will catch it and show an alertbox and changing to another tab. This is my logic. But it is not executing the catch block. It is going straight to the webview and showing there a null value. Please anyone help me to fix this error.Here is my class...
LocationActivity
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;
@SuppressLint("SetJavaScriptEnabled")
public class LocationActivity extends Activity {
MainTabActivity mainTab;
WebView mWebView;
GPSTracker g;
double my_lat, my_lon;
String lat, lon, to_lat, to_lon, addressText, temp_lat, temp_lon;
DatabaseHandler db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DatabaseHandler(this);
g = new GPSTracker(this);
Log.d("Reading: ", "Location Activity Reading all Values");
List<Datas> datas = db.getAllDatas();
for (Datas dat : datas) {
try{
to_lat = dat.getlat();
to_lon = dat.getlon();
}catch(NullPointerException e){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Oops!");
// set dialog message
alert
.setMessage("Please select a destination before finding your route!")
.setCancelable(false)
.setPositiveButton("ok",new DialogInterface.OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(DialogInterface dialog,int id) {
((MainTabActivity)getParent()).getTabHost().setCurrentTab(0);
}
});
// create alert dialog
AlertDialog alertDialog = alert.create();
// show it
alertDialog.show();
Log.d(to_lat, to_lon);
System.out.println("Frim thap thee "+ to_lat + " " + to_lon);
}
}
setContentView(R.layout.webview);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON);
final ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1);
pb.setVisibility(View.VISIBLE);
my_lat = g.getLatitude();
my_lon = g.getLongitude();
lat = String.valueOf(my_lat);
lon = String.valueOf(my_lon);
mWebView = (WebView)findViewById(R.id.webview1);
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
pb.setProgress(progress);
if (progress == 100) {
pb.setVisibility(View.GONE);
}
}
});
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
addressText = lat + "," + lon + "+to:" + to_lat + ","+ to_lon;
addressText = addressText.replaceAll(" ", "%20");
addressText = addressText.replaceAll("#", "%23");
mWebView.loadUrl("http://maps.google.com/maps?q=from:"+ addressText);
}
}
getlat
and getlon
may be null, and therefore not throw an exception
. You should replace your try/catch
with a if
statement which checks for null
.
Furthermore the real problem of you NullPointerException
is:
Log.d(to_lat, to_lon);
Log.d
should be used with the TAG
as first param, and the String
as second:
Log.d("LOCATION", "From " + to_lat + ", to " + to_lon);
If to_lat
and to_lon
are null
:
log.d(null, null);
will definately cause a crash.