0

这是一个 sdk 设置为的 Android 应用程序:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11"/>

问题是该应用程序将在 AVD 上的 Eclipse 环境中正常运行。当我直接安装到手机或什至将手机设置为在 Eclipse 中使用的设备时,应用程序在以下代码区域中失败。为了缩小错误发生的范围,我沿途放置了一些吐司,“点击”吐司显示,但“意图创建”没有。所以这让我只有 2 行,但我无法弄清楚为什么它会在那个区域死去。带有toast的方法的代码如下:

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 类的 bundle 方法如下:

  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();

此处的 BusinessDetails 被截断,因为从未到达以下行:

   Toast.makeText(getApplicationContext(), "check 1", Toast.LENGTH_LONG).show();

这告诉我,代码甚至永远都达不到......有什么想法会出错吗???

LogCat 异常:

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)

这是处理网络的 ASYNC 任务:

     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();
    }
}
4

2 回答 2

2

如果没有堆栈跟踪,我会把钱赌在两件事上:

  1. 在 onCreate Activity 仍在启动,您已经创建了一个 Toast

  2. 使用 Activity 上下文而不是 Appcontext 通常会更好地处理视觉内容

无论如何,stacktrace会很好

com.example.businesslookup.BusinessDetails$GetBusinessDetails$1.run(BusinessDetails.java:127)

在这一行,有你的问题

也请阅读:http: //developer.android.com/reference/android/os/NetworkOnMainThreadException.html

于 2012-10-27T21:43:32.207 回答
1

您正在使用 AsyncTask,但您还将代码包装在调用中,runOnUiThread()这违背了使用 AsyncTask 的目的。尝试删除此包装器并将 UI 访问权限移至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();
    }
}

(我不知道这段代码是否可以编译,但这足以给你正确的想法。)

于 2012-10-27T22:03:11.957 回答