2016-06-10 95 views
0

我有應用程序使用PHP添加新的數據庫到數據庫。如果我在程序中單擊按鈕創建ID它總是關閉除插入數據庫中的數據之外的所有內容。Android應用程序關閉後點擊按鈕

我不知道如何解決它。在我的logcat中沒有關於任何錯誤的信息。

這是我的代碼:

Toolbar toolbar; 
// Progress Dialog 
private ProgressDialog pDialog; 

JSONParser jsonParser = new JSONParser(); 
EditText inputusername; 
EditText inputpassword; 
EditText inputnamegm; 
TextView registerErrorMsg; 
Button createidgm; 
// url to create news 
private static String url_create_idgm = "http://192.168.1.111/add/create_idgm.php"; 

// JSON Node names 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_ERROR = "error"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_create_idgm); 

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    TypedValue typedValueColorPrimaryDark = new TypedValue(); 
    CreateIDGM.this.getTheme().resolveAttribute(R.attr.colorPrimary, typedValueColorPrimaryDark, true); 
    final int colorPrimaryDark = typedValueColorPrimaryDark.data; 
    if (Build.VERSION.SDK_INT >= 21) { 
     getWindow().setStatusBarColor(colorPrimaryDark); 
    } 

    // Edit Text 
    inputusername = (EditText) findViewById(R.id.inputusername); 
    inputpassword = (EditText) findViewById(R.id.inputpassword); 
    inputnamegm = (EditText) findViewById(R.id.inputname); 
    registerErrorMsg = (TextView) findViewById(R.id.error); 
    // Create button 
    createidgm = (Button) findViewById(R.id.btnCreate); 

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

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

/** 
* Background Async Task to Create new product 
* */ 
class NetCheck extends AsyncTask<String, String, String> { 

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

    /** 
    * Creating product 
    * */ 
    protected String doInBackground(String... args) { 
     String name = inputusername.getText().toString(); 
     String version = inputpassword.getText().toString(); 
     String description = inputnamegm.getText().toString(); 

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

     // getting JSON Object 
     // Note that create product url accepts POST method 
     JSONObject json = jsonParser.makeHttpRequest(url_create_idgm, 
       "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(CreateIDGM.this, "Success Create ID Game Master ", Toast.LENGTH_SHORT).show(); 
       Intent i = new Intent(getApplicationContext(), ListTopGM.class); 
       startActivity(i); 

       // closing this screen 
       finish(); 
      } else { 
       // failed to create product 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog once done 
     pDialog.dismiss(); 
    } 
} 

有人可以幫我解決這個問題?

回答

0

doInBackground不是ui線程的一部分。所以你不能做任何關於UI的事情,因爲你的例子開始一個新的屏幕是一個UI工作,但你在回線程中調用它,這會導致崩潰。所以只需將你的startActivity塊移動到onPostExecute。另外,finish()會關閉整個屏幕。最後一件事,你的catch塊只能捕獲JSONException,在你的情況下這還不夠。如果您將其更改爲Exception,則會在logcat中看到異常日誌。

相關問題