2013-03-23 53 views
0

我在eclipse中編寫了一個java代碼,可以讓我在本地數據庫上創建一個產品..實際上它的工作,但是當它完成時,應用程序崩潰,如果我去我的數據庫我可以看到創建的產品 任何人都可以幫助我嗎?android註冊表崩潰

public class MainActivity extends Activity { 

// Progress Dialog 
    private ProgressDialog pDialog; 

    JSONParser jsonParser = new JSONParser(); 
    EditText inputName; 
    EditText inputPrice; 
    EditText inputDesc; 

    // url to create new product 
    private static String url_create_product = "http://10.0.2.2/android_connect/create_product.php" ; 

    // JSON Node names 
    private static final String TAG_SUCCESS = "success"; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

// Edit Text 
     inputName = (EditText) findViewById(R.id.inputName); 
     inputPrice = (EditText) findViewById(R.id.inputPrice); 
     inputDesc = (EditText) findViewById(R.id.inputDesc); 

     // Create button 
     Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct); 

     // button click event 
     btnCreateProduct.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       // creating new product in background thread 
       new CreateNewProduct().execute(); 
      } 
     }); 

} 

class CreateNewProduct extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Creating Product.."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 



    protected String doInBackground(String... args) { 
     String name = inputName.getText().toString(); 
     String price = inputPrice.getText().toString(); 
     String description = inputDesc.getText().toString(); 

     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("name", name)); 
     params.add(new BasicNameValuePair("price", price)); 
     params.add(new BasicNameValuePair("description", description)); 

     // getting JSON Object 
     // Note that create product url accepts POST method 
     JSONObject json = jsonParser.makeHttpRequest(url_create_product, 
       "POST", params); 

     // check log cat fro response 
     Log.d("Create Response", json.toString()); 

     // check for success tag 
     try { 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // successfully created product 
       Toast.makeText(getBaseContext(), "Succes!", Toast.LENGTH_SHORT).show(); 

       // closing this screen 
       finish(); 
      } else { 
       // failed to create product 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog once done 
     pDialog.dismiss(); 
    } 

} 

}

+1

你告訴我們它墜毀了,但你不告訴我們崩潰是什麼或告訴我們logcat。我們該如何猜測? – Simon 2013-03-23 13:05:37

+0

錯誤堆棧跟蹤會給出更好的崩潰。那麼我們可以擁有logcat嗎? – SudoRahul 2013-03-23 13:05:40

+0

可能你得到這個異常'CalledFromWrongThreadException:只有創建視圖層次結構的原始線程...'因爲你試圖在doInBackground中顯示Toast和訪問UI元素 – 2013-03-23 13:18:17

回答

0

移動此行

 Toast.makeText(getBaseContext(), "Succes!", Toast.LENGTH_SHORT).show(); 

您onPostExecute()方法。

+0

謝謝你的人,它的工作很好 – Charbel 2013-03-23 13:47:34

+0

但我有一個問題,當它告訴我成功messg應用程序已關閉...我怎麼能讓它,當它顯示我messg應用程序去我的應用程序項目中的另一個頁面? – Charbel 2013-03-23 13:49:20

+0

問另一個問題。評論不是延伸問答的地方。另外,對話是邪惡的。 :) – 2013-03-23 19:52:27