Reputation: 1559
This is a Android application with sdk set as:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11"/>
The problem is that the app will run fine in the Eclipse Environment on a AVD. When I install directly to the phone or even set the phone as the device to use in Eclipse the app fails in the below area of code. To narrow down where the error was occurring I placed some toast's along the way and the "clicked" toast displays but the "Intent Created" does not. So that leaves me with only 2 lines, but I can not figure out for the life of me why it dies in that area. Code of the method with the toast's in it is as follows:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String bid = ((TextView) view.findViewById(R.id.bid)).getText()
.toString();
Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show();
Intent in = new Intent(getApplicationContext(),
BusinessDetails.class);
in.putExtra(TAG_BID, bid);
Toast.makeText(getApplicationContext(), "Intent Created", Toast.LENGTH_LONG).show();
startActivityForResult(in, 100);
}
});
BusinessDetails Class to the bundle method is as follows:
package com.example.businessLookUp;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
public class BusinessDetails extends Activity {
EditText txtDesc;
Button btnSave;
String bid;
String _bname;
String _phone;
String _address;
String _city;
String _state;
String _zip;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String url_business_detials = "http://192.168.1.2/get_business_details.php";
private static final String url_update_details = "http://192.168.1.2/update_business.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_BUSINESS = "business";
private static final String TAG_BID = "bid";
private static final String TAG_NAME = "name";
private static final String TAG_ADDRESS = "address";
private static final String TAG_CITY = "city";
private static final String TAG_STATE = "state";
private static final String TAG_ZIP = "zip";
private static final String TAG_PHONE = "phone";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_business);
// save button
btnSave = (Button) findViewById(R.id.btnSave);
Intent i = getIntent();
bid = i.getStringExtra(TAG_BID);
Toast.makeText(getApplicationContext(), "check 1", Toast.LENGTH_LONG).show();
// Getting complete product details in background thread
new GetBusinessDetails().execute();
The BusinessDetails was truncated here because the line below is never reached:
Toast.makeText(getApplicationContext(), "check 1", Toast.LENGTH_LONG).show();
which tells me that the code is never even reached... Any ideas where this is going wrong at???
LogCat Exception:
10-27 16:39:28.834: E/AndroidRuntime(25260): FATAL EXCEPTION: main
10-27 16:39:28.834: E/AndroidRuntime(25260): android.os.NetworkOnMainThreadException
10-27 16:39:28.834: E/AndroidRuntime(25260): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1119)
10-27 16:39:28.834: E/AndroidRuntime(25260): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-27 16:39:28.834: E/AndroidRuntime(25260): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-27 16:39:28.834: E/AndroidRuntime(25260): at libcore.io.IoBridge.connect(IoBridge.java:112)
10-27 16:39:28.834: E/AndroidRuntime(25260): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-27 16:39:28.834: E/AndroidRuntime(25260): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-27 16:39:28.834: E/AndroidRuntime(25260): at java.net.Socket.connect(Socket.java:842)
10-27 16:39:28.834: E/AndroidRuntime(25260): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-27 16:39:28.834: E/AndroidRuntime(25260): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
10-27 16:39:28.834: E/AndroidRuntime(25260): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-27 16:39:28.834: E/AndroidRuntime(25260): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-27 16:39:28.834: E/AndroidRuntime(25260): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-27 16:39:28.834: E/AndroidRuntime(25260): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-27 16:39:28.834: E/AndroidRuntime(25260): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-27 16:39:28.834: E/AndroidRuntime(25260): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-27 16:39:28.834: E/AndroidRuntime(25260): at com.example.businesslookup.JSONParser.makeHttpRequest(JSONParser.java:65)
10-27 16:39:28.834: E/AndroidRuntime(25260): at com.example.businesslookup.BusinessDetails$GetBusinessDetails$1.run(BusinessDetails.java:127)
10-27 16:39:28.834: E/AndroidRuntime(25260): at android.os.Handler.handleCallback(Handler.java:605)
10-27 16:39:28.834: E/AndroidRuntime(25260): at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 16:39:28.834: E/AndroidRuntime(25260): at android.os.Looper.loop(Looper.java:137)
10-27 16:39:28.834: E/AndroidRuntime(25260): at android.app.ActivityThread.main(ActivityThread.java:4697)
10-27 16:39:28.834: E/AndroidRuntime(25260): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 16:39:28.834: E/AndroidRuntime(25260): at java.lang.reflect.Method.invoke(Method.java:511)
10-27 16:39:28.834: E/AndroidRuntime(25260): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
10-27 16:39:28.834: E/AndroidRuntime(25260): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
10-27 16:39:28.834: E/AndroidRuntime(25260): at dalvik.system.NativeStart.main(Native Method)
And this is the ASYNC task that handles the networking:
class GetBusinessDetails extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(BusinessDetails.this);
pDialog.setMessage("Loading details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... params) {
runOnUiThread(new Runnable() {
public void run() {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("bid", bid));
JSONObject json = jsonParser.makeHttpRequest(
url_business_detials, "GET", params);
Log.d("Single Business Details", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray businessObj = json.getJSONArray(TAG_BUSINESS); // JSON Array
JSONObject business = businessObj.getJSONObject(0);
TextView _bName = (TextView) findViewById(R.id.bnameInf);
TextView _phone = (TextView) findViewById(R.id.phoneInf);
TextView _address = (TextView) findViewById(R.id.addressInf);
TextView _city = (TextView) findViewById(R.id.cityInf);
TextView _state = (TextView) findViewById(R.id.stateInf);
TextView _zip = (TextView) findViewById(R.id.zipInf);
_bName.setText(business.getString(TAG_NAME));
_phone.setText(business.getString(TAG_PHONE));
_address.setText(business.getString(TAG_ADDRESS));
_city.setText(business.getString(TAG_CITY));
_state.setText(business.getString(TAG_STATE));
_zip.setText(business.getString(TAG_ZIP));
}else{
Toast.makeText(getApplicationContext(), "No Match Returned", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
Upvotes: 0
Views: 202
Reputation: 86948
You are using an AsyncTask, but you're also wrapping your code in a call to runOnUiThread()
which defeats the purpose of using an AsyncTask. Try removing this wrapper and moving the UI access to onPostExecute()
:
protected String doInBackground(String... params) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("bid", bid));
JSONObject json = jsonParser.makeHttpRequest(
url_business_detials, "GET", params);
Log.d("Single Business Details", json.toString());
success = json.getInt(TAG_SUCCESS);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (success == 1) {
JSONArray businessObj = json.getJSONArray(TAG_BUSINESS); // JSON Array
JSONObject business = businessObj.getJSONObject(0);
TextView _bName = (TextView) findViewById(R.id.bnameInf);
TextView _phone = (TextView) findViewById(R.id.phoneInf);
TextView _address = (TextView) findViewById(R.id.addressInf);
TextView _city = (TextView) findViewById(R.id.cityInf);
TextView _state = (TextView) findViewById(R.id.stateInf);
TextView _zip = (TextView) findViewById(R.id.zipInf);
_bName.setText(business.getString(TAG_NAME));
_phone.setText(business.getString(TAG_PHONE));
_address.setText(business.getString(TAG_ADDRESS));
_city.setText(business.getString(TAG_CITY));
_state.setText(business.getString(TAG_STATE));
_zip.setText(business.getString(TAG_ZIP));
}else{
Toast.makeText(getApplicationContext(), "No Match Returned", Toast.LENGTH_LONG).show();
}
}
(I don't know if exact this code will compile, but it's enough to give you the right idea.)
Upvotes: 1
Reputation: 2284
Without a stacktrace i would bet my money on 2 things:
in onCreate the Activity is still starting up and you already create a Toast
visual stuff is often better off with an Activity Context instead of the Appcontext
anyway, stacktrace would be nice
com.example.businesslookup.BusinessDetails$GetBusinessDetails$1.run(BusinessDetails.java:127)
in this line, there is your problem
also please read: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
Upvotes: 2