2012-10-27 63 views
0

工作完全這與SDK中的Android應用程序設置爲當:Android應用程序在法墜毀實際設備上使用,但在Eclipse

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

的問題是,應用程序將在Eclipse環境上運行良好AVD。當我直接安裝到手機中,或者甚至將手機設置爲在Eclipse中使用的設備時,該應用程序在下面的代碼區域中失敗。我對Java依然陌生,更不用說爲Android而努力,所以請隨身攜帶,因爲在調試方面我仍然有點失落......但爲了縮小發生錯誤的位置,我一路上放了一些吐司,「點擊「吐司顯示,但」創建的意圖「不......因此,我只有2行......但我無法弄清楚爲什麼它死於該領域的生活...方法代碼烤麪包在其計算方法如下:

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類的束方法如下:

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) 

這是處理網絡異步任務:

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

看看你的logcat輸出。在那裏你會看到堆棧跟蹤。在上面的代碼中加入並標記出現異常的行。 –

+0

您正在嘗試在UI線程上執行網絡命令,請查看「NetworkOnMainThreadException」。 – Sam

+0

再次確定我是新的..使感覺。那麼我將如何去在UI線程之外執行它們呢? – Skindeep2366

回答

1

您正在使用AsyncTask,但您也在打電話給runOnUiThread()打包代碼,這違背了使用AsyncTask的目的。嘗試刪除該包裝和移動到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(); 
    } 
} 

(我不知道確切這段代碼可以編譯,但它足以給你正確的想法。)

+0

非常感謝......我自己從來沒有想過,因爲我自己是一個vb程序員,所以這一切都是不穩固的地面...... – Skindeep2366

2

沒有一個堆棧跟蹤我會賭兩件事情我的錢:

  1. 在onCreate活動仍在啓動,您已經創建了Toast

  2. 視覺的東西是經常有活動上下文而不是Appcontext

反正更好,堆棧跟蹤將是很好

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

在這一行

,還有就是你的問題

也請閱讀: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

+0

Stacktrace已被添加到OP – Skindeep2366

相關問題